简体   繁体   中英

NumberFormatException thrown by Integer.parseInt

Hey Im taking coding lessons at school but the teacher does not explain that well so we have to look for info online which I did, but I was not able to find the error in my code, can you help me please?

char end='s';     
do{
    System.out.println("Tipo de boleto");
    char boleto = (char) System.in.read();
    switch (boleto){
        case 'a':
          System.out.println("El boleto cuesta $120.00");
          System.out.println("Otro boleto (s/n)?");
          end = (char) Integer.parseInt(entrada.readLine());
          continue;
    case 'n':
          System.out.println("El boleto cuesta $75.00");
          System.out.println("Otro boleto (s/n)?");
          end = (char) Integer.parseInt(entrada.readLine());
          continue;
    case 'i':
          System.out.println("El boleto cuesta $60.00");
          System.out.println("Otro boleto (s/n)?");
          end = (char) Integer.parseInt(entrada.readLine());;
          continue;
    default:
          System.out.println("Error" );
          break;
    }
}
  while (end == 'n');

Exception

    run: Tipo de boleto a El boleto cuesta $120.00 Otro boleto (s/n)? 

Exception in thread "main" java.lang.NumberFormatException: For input string: "" at 

java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.lang.Integer.parseInt(Integer.java:592) at 

java.lang.Integer.parseInt(Integer.java:615) at 
asjidbhahsjksbd.Asjidbhahsjksbd.main(Asjidbhahsjksbd.java:16) Java Result: 1 

BUILD SUCCESSFUL (total time: 7 seconds)

See, you are trying to parse "" as an Integer whichwill throw NumberFormatException . You have to check for null and isEmpty() in this order and then try to parse the string as an integer.

you should replace continue statement with a break. putting continue will skip the current iteration and the while condition will not be evaluated.

You are getting exception in this line , i think you are getting "" blank String from readLine() method

 end = (char) Integer.parseInt(entrada.readLine());

So Do like this

String input=entrada.readLine();
if(input!=null && !input.equals(""))
{
 end = (char) Integer.parseInt(input);
}

I suggest you to use google guava libraries which is having a utility function

Strings.isNullOrEmpty(inputString)//Checks String for both null and empty

Update As @ajb suggested :

If you want to convert s and n into character than don't use your code snippet

instead of Parsing an Integer

Use

char c=input.charAt(0);

This does not do what you think it will:

end = (char) Integer.parseInt(entrada.readLine());

This line reads a string. It then assumes the string is a number , and determines the number. If the user actually enters "s" or "n" , it throws an exception, because "s" and "n" are not numbers. The number is then treated as the ASCII value of a character. The result is that the loop will test whether the user types in the string "110" , since 110 is the ASCII value of the character n .

There are several ways to fix this; here's one:

end = entrada.readLine().charAt(0);

This returns the first character of whatever line the user types in. This is a sloppy solution because it doesn't work if the user hits ENTER on an empty line (it will throw an exception). Better:

String answer = entrada.readLine();  
if (answer.isEmpty()) {
    end = 'n';        // treat an empty string like 'n'
} else {
    end = answer.charAt(0);
}

Also, I think the while might be wrong. while (end == 'n') means the program will loop back if the user enters n , which I think is the opposite of what you want.

PS There are other errors that I didn't catch, that others have pointed out; using continue is wrong--use break to leave the switch statement. And reading one character with System.in.read() is a problem, because the user will type in a character, but the character won't get into the program until the user types ENTER, and then readLine() will get the rest of this first line, instead of asking for another line. But I usually don't use System.in.read() so I'm not completely sure what this does without trying it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM