3 Replies - 17612 Views - Last Post: 17 August 2008 - 02:41 PM Rate Topic: -----

#1 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Number Format Exception

Posted 17 August 2008 - 05:47 AM

	private void test03() {
		try{
		System.out.print("Enter any number except 7: ");
		int num2 = Integer.parseInt(Keyboard2.readInput() + 1);
		if(num2 == 7){
			throw new ArithmeticException();
		}else{
		int num1 = 255 / (num2 - 71);

		System.out.print("Enter a String of digits: ");
		String numS = Keyboard2.readInput();
		int num3 = Integer.parseInt(numS);
		System.out.println("Statement D");
		throw new NumberFormatException();


			}

			}catch(NumberFormatException e) {
				System.out.println("CAUGHT: " + e.toString());
			}
			catch(ArithmeticException e) {
				System.out.println("CAUGHT: " + e.toString() +  "/ by zero");
			} finally{
			System.out.println("Statement E");
				}
			} 



ok this is the code I got. Suppose when I type in 345 it should just run normally, but when I type something like 12abc it will throw in a number format exception (this is how it should work). But when I test run it, it give me the output the other way round, which is it gives me a number format exception when I type in 345, but works normally if I type in 12abc...... what should I do??

Secondly when I use the code
 static final String lineSeparator = System.getProperty ( "line.separator" );

do I replace the system.out.print with system.getProperty? As well as if I use the line separator do I still need to type the /r/n function?

Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Number Format Exception

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8334
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Number Format Exception

Posted 17 August 2008 - 08:33 AM

Don't really understand what you try to achieve here.

num2 and num3 are read but never used

one thing for sure:

int num2 = Integer.parseInt(Keyboard2.readInput() + 1);

should be

int num2 = Integer.parseInt(Keyboard2.readInput()) + 1;

The way your try/catch are implemented you'll never know which statement fires the exception better to separate them

int num1 = 0, num2 = 0, num3 = 0;

System.out.printl("Enter a number but 7: ");
try {
   num2 = Integer.parseInt(Keyboard2.readInput()) + 1;
}
catch(NumberFormatException e) {
   System.out.println("Exception reading num2: " + e);
}

....

String numS = Keyboard2.readInput();
try {
   num3 = Integer.parseInt(numS);
}
catch(NumberFormatException e) {
   System.out.println("Exception reading num3: " + e);
}


Was This Post Helpful? 0
  • +
  • -

#3 thenovices  Icon User is offline

  • D.I.C Head

Reputation: 9
  • View blog
  • Posts: 74
  • Joined: 19-January 08

Re: Number Format Exception

Posted 17 August 2008 - 08:49 AM

View Posttombfighter, on 17 Aug, 2008 - 05:47 AM, said:

Secondly when I use the code
 static final String lineSeparator = System.getProperty ( "line.separator" );

do I replace the system.out.print with system.getProperty? As well as if I use the line separator do I still need to type the /r/n function?

Thanks!


You're using System.out.println, which I believe takes care of the lineSeparator for you. There is no need to do all that system.getProperty stuff.
Was This Post Helpful? 0
  • +
  • -

#4 tombfighter  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 14-August 08

Re: Number Format Exception

Posted 17 August 2008 - 02:41 PM

View Postpbl, on 17 Aug, 2008 - 08:33 AM, said:

Don't really understand what you try to achieve here.

num2 and num3 are read but never used

one thing for sure:

int num2 = Integer.parseInt(Keyboard2.readInput() + 1);

should be

int num2 = Integer.parseInt(Keyboard2.readInput()) + 1;

The way your try/catch are implemented you'll never know which statement fires the exception better to separate them

int num1 = 0, num2 = 0, num3 = 0;

System.out.printl("Enter a number but 7: ");
try {
   num2 = Integer.parseInt(Keyboard2.readInput()) + 1;
}
catch(NumberFormatException e) {
   System.out.println("Exception reading num2: " + e);
}

....

String numS = Keyboard2.readInput();
try {
   num3 = Integer.parseInt(numS);
}
catch(NumberFormatException e) {
   System.out.println("Exception reading num3: " + e);
}



What I meant what if everything was correct, when I run the programe it will ask me to input a string of digits. When I input something like 123456, the output the programe will give will be:
statement D
statement E
finish

But if I input something like 123abc the output of the programe will be:
CAUGHT:java.lang.NumberFormatException in the string: 123abc

If everything was correct it should give something like that. Yet when I tried to run my programme, the output gave me the opposite response. Meaning when I 123456, the output gaved me a number format exception. While I input something like 123abc the output gave me:
statement D
statement E
finish


So I wounder what I did wrong
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1