Jump to content

Welcome to Geeks to Go - Register now for FREE

Need help with your computer or device? Want to learn new tech skills? You're in the right place!
Geeks to Go is a friendly community of tech experts who can solve any problem you have. Just create a free account and post your question. Our volunteers will reply quickly and guide you through the steps. Don't let tech troubles stop you. Join Geeks to Go now and get the support you need!

How it Works Create Account
Photo

Java Exceptions


  • Please log in to reply

#1
SOORENA

SOORENA

    Member

  • Member
  • PipPipPip
  • 974 posts
Ok I'm new to java and I learned how to do try and catch statements and a lot of other stuff. I'm trying to catch the user from enter a negative number or a number above 100. How do I do that? Here is a part of my program where I'm putting that in:

public void findbuttonActionPerformed (ActionEvent event) throws IOException
	{
		try
		{
			value1 = Double.parseDouble (Test1_Text.getText ());
			value2 = Double.parseDouble (Test2_Text.getText ());
			value3 = Double.parseDouble (Test3_Text.getText ());
			value4 = Double.parseDouble (Exam_Text.getText ());

		}
		catch (NumberFormatException e)
		{
			JOptionPane.showMessageDialog (null, "Please Enter Right Value!!!");
			value1 = Double.parseDouble (Test1_Text.getText ());
			value2 = Double.parseDouble (Test2_Text.getText ());
			value3 = Double.parseDouble (Test3_Text.getText ());
			value4 = Double.parseDouble (Exam_Text.getText ());
		}

	}

I thought maybe it was the NumberFormatException but apparently not.

Thanks in advance.

Soorena

Edited by SOORENA, 22 May 2007 - 07:08 PM.

  • 0

Advertisements


#2
stettybet0

stettybet0

    Trusted Tech

  • Technician
  • 2,579 posts
i dont know that an exception is really needed to do this.

Here's of an example of how I would do this using the command line:

import java.io.*;

public class number {

   public static void main (String[] args) {

	  System.out.print("Enter a number between 0 and 100: ");

	  BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

	  String inputString = null;
	  int inputInteger = 0;

	  try {
		 inputString = br.readLine();
		 inputInteger = Integer.parseInt(inputString);
	  } catch (IOException ioe) {
		 System.out.println("Error!");
		 System.exit(1);
	  }

	  // The above is NOT the exception you are looking for, it just makes sure the input # was properly recieved.

	  if (inputInteger < 0 || inputInteger > 100) {
		 System.out.println("Please enter a valid number!");
	  } else {
		 System.out.println(inputInteger);
	  }
   }
}

The part you want to focus on is:

if (inputInteger < 0 || inputInteger > 100) {
		 System.out.println("Please enter a valid number!");
	  } else {
		 System.out.println(inputInteger);
	  }

Any questions, just ask.
  • 0

#3
coyne20

coyne20

    Member

  • Member
  • PipPip
  • 35 posts
Another alternative is to use assert statements. This may be a little too advanced for you right now. So i strongly suggest that you stick with the previous users suggestion of an if-then-else validation code block.
  • 0






Similar Topics

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

As Featured On:

Microsoft Yahoo BBC MSN PC Magazine Washington Post HP