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)
you should replace continue statement with a break. putting continue will skip the current iteration and the while condition will not be evaluated.
This does not do what you think it will:
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 charactern
.There are several ways to fix this; here's one:
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:
Also, I think the
while
might be wrong.while (end == 'n')
means the program will loop back if the user entersn
, which I think is the opposite of what you want.P.S. There are other errors that I didn't catch, that others have pointed out; using
continue
is wrong--usebreak
to leave theswitch
statement. And reading one character withSystem.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 thenreadLine()
will get the rest of this first line, instead of asking for another line. But I usually don't useSystem.in.read()
so I'm not completely sure what this does without trying it.You are getting exception in this line , i think you are getting
""
blank String fromreadLine()
methodSo Do like this
I suggest you to use google guava libraries which is having a utility function
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
See, you are trying to parse
""
as an Integer whichwill throwNumberFormatException
. You have to check fornull
andisEmpty()
in this order and then try to parse the string as an integer.