This week's book giveaway is in the Go forum.
We're giving away four copies of Head First Go and have Jay McGavren on-line!
See this thread for details.
Win a copy of Head First Go this week in the Go forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Issue on if/else if/else comment  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, i am a beginner of java and this is a scrip/app that i wrote. Now i facing a problem that when i type in "QUIT" it stop the scrip/app however it pop up this error message
Exception in thread "main" java.lang.NumberFormatException: For input string: "QUIT"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at scanner1.Scanner1.main(Scanner1.java:35)

How can i solve this problem? Also how should i detect user input is not number or "QUIT" and print a message to ask them to key in a number or "QUIT".



 
Saloon Keeper
Posts: 2408
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this line: int grade = Integer.parseInt(output); you are trying to parse the input to get an int value, which is failing because QUIT does not represent an int value.  Try using Integer.parseInt after you have checked for QUIT.
 
Ron McLeod
Saloon Keeper
Posts: 2408
296
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And Welcome to the Ranch!
 
Alvin Lim Jun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ron McLeod wrote:In this line: int grade = Integer.parseInt(output); you are trying to parse the input to get an int value, which is failing because QUIT does not represent an int value.  Try using Integer.parseInt after you have checked for QUIT.


Thanks for helping and I will try that later😊😊
 
Marshal
Posts: 63489
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch again.

I think your question is so closely related to your first thread that the two would be better merged into one discussion.
 
Alvin Lim Jun
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,i am here with another question.
Things that i want to achieve:
allow user to key in marks
allow user to key in "QUIT" when they want to stop
a message of "Enter a valid number or QUIT" when user enter invalid number or word like "q,quit,opopad,.ect"
Now i strugger at the third part,hope someone could help me with this

 
Sheriff
Posts: 13183
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This:

is both confusing and pointless.

On line 28, why did you choose the name output? Is this value really output? Wouldn't a name like response make more sense? Choose names that accurately reflect what it is they represent so that people reading your code don't get confused or get the wrong idea of what's going on.

Why do you feel you have to call String.valueOf(output)? All you're doing is creating another copy of the string.

Here's what's happening in your code:

28: get user's response as a String
29: convert the user's response (which is already a String) to a String (Again, why do this at all?)
30: convert the user's response to an integer (This still won't work if user's response is "QUIT")

What you probably want to do is this:

get user's response as a String
see if user's response is "QUIT" then quit the program
otherwise, convert the response to an integer
 
Junilu Lacar
Sheriff
Posts: 13183
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, if you're going to wait for the user to respond "QUIT" to signal that the program should end, why are you using a for-loop with a fixed number of iterations?  For loops are used when you know exactly how many times at most you want to repeat some set of statements.  If you don't know how many times up front but will allow an indefinite number of repeats until the user responds in a certain way, then the more appropriate loop structure is a while-loop or a do-while loop.
 
Campbell Ritchie
Marshal
Posts: 63489
207
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Junilu has told you, you are having problems with inappropriate types of loop. Even if you are going to use a for loop, don't use 1 and don't use <=. Always start of writing a for loop like this:-Note:-
  • 1: Start from 0.
  • 2: Count to less than something.
  • 3: No semicolon after ).
  • 4: Always use {} even if the loop body is one line.
  • 5: If you do something different, e.g. comparing two adjacent array elements, you will have to change the header and you then might want ...int i = 1;... or <= after all.
  • Again, your for loop runs 15×, and that probably isn't what you want.
    Avoid nextLine(). You probably don't really want to read the whole of the line, and as you have seen it is awkward to turn that line into an int, and there is a potential pitfall if you use nextLine() after nextAnythingElse(). Go through the Scanner documentation and look for methods to make things easier. Maybe hasNextInt() and nextInt().
     
    Master Rancher
    Posts: 1162
    18
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're also better to check ignoring the casesensitivity (comment.equalsIgnoreCase("QUIT")), cause now when the user types Quit or quit, the program will still keep running, but you'll get an exeption when trying to parse to an integer. In this line:

    you don't need to check the comment anymore, cause if it would have been QUIT the first if would have been executed.

    one of these variables is redundant
     
    Daniel Demesmaecker
    Master Rancher
    Posts: 1162
    18
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Junilu I think he loops 5 times cause he expects 5 grades, see the calculation for average.
    @op: Maybe it would be cleaner to keep a counter and devide by the counter to calculate the average, doing that the user can enter as mutch grades as he wants and the calculation will still be correct.
    I would also calculate the average outside the loop, instead of reasigning it each time
     
    Campbell Ritchie
    Marshal
    Posts: 63489
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Daniel Demesmaecker wrote:. . . . . .

    How on earth are you going to have a mark < 0? Will that if ever be entered?
     
    Daniel Demesmaecker
    Master Rancher
    Posts: 1162
    18
    Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If the user(and with user I don't mean the op, I'm talking about in production, who ever uses the program) is stupid enough to enter a negative number....
     
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hello,
    You have a few options here, and if you want to keep everything in the main method, you can handle the exception here. Like in the following code:


     

    I hope this helps.

    Please let me know if you want to know more about alternatives.

    Best Regards
    Alin
     
    Campbell Ritchie
    Marshal
    Posts: 63489
    207
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Alin Bistrian wrote:. . . if you want to keep everything in the main method . . .

    Though it is usually not a good idea to have so much all in the main() method.
     
    Bartender
    Posts: 5652
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This line
    should be moved  to after the loop completes. It makes no sense where it is because until the loop completes you don't have '5' and you have no need to print it.
     
    Carey Brown
    Bartender
    Posts: 5652
    56
    Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You don't need 'valueOf()' because 'output' is already a String. Replace with
    I would also suggest using equalsIgnoreCase() so that a user could enter a lower case "quit".
     
    Alin Bistrian
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:This line
    should be moved  to after the loop completes. It makes no sense where it is because until the loop completes you don't have '5' and you have no need to print it.



    Yes, that is correct. There is no need for computation until the loop has finished. Good spot!
     
    Alin Bistrian
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Alin Bistrian wrote:. . . if you want to keep everything in the main method . . .

    Though it is usually not a good idea to have so much all in the main() method.



    Correct. I would take out everything from the main method. There are some other mistakes as well as others have pointed out but, I so that he needs to understand the flow first.
     
    Alin Bistrian
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am not sure how much we have helped Alvin with our comments.

    That is why I have decided to dedicate an entire blog series to refactoring, and the first post is the code that he posted here.

    You can read here: How to Refactor Java Main Method Code

    This post will give you a hint on how to separate functionalities to avoid reading code that is not related to what you are looking for.

    If you have any questions, please let me know, and I will be happy to help.

    Best Regards
    Alin Bistrian
     
    Campbell Ritchie
    Marshal
    Posts: 63489
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Only looked at part of your blog, but it looked useful. In one place you said line 19 when I think you meant line 20.
     
    Alin Bistrian
    Greenhorn
    Posts: 17
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you very much for reading the post. I am glad that you found it useful.

    In regards to line numbers, the statement I was referring to is String comment = String.valueOf(output); which is on line 19.


    Regards
    Alin
     
    Campbell Ritchie
    Marshal
    Posts: 63489
    207
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, it is on line 20. It is also on line 19 and line 24 depending which code block you look at. But the code block nearest to where you said 19 has it on line 20.
     
    Doody calls. I would really rather that it didn't. Comfort me wise and sterile tiny ad:
    Become a Java guru with IntelliJ IDEA
    https://www.jetbrains.com/idea/
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!