• Post Reply Bookmark Topic Watch Topic
  • New Topic

Loop back when input not an integer?  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi so my program works fine but there are some exceptions which i am not sure how to deal with, like how do i make it such that when the choice is not an integer but a character, it will loop back to the menu. I am new to java so it would be great if you could guide me


This is the result that i got when i tried a character

 
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is input? I assume it is a reference to an instance of Scanner class.
In that case see this: Scanner#hasNextInt
 
Roy Poh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is the only input scanner i have


Should I add another one?
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, don't add more Scanners. One is fine. Just use the method hasNextInt. I posted a link which might help you.
 
Roy Poh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sorry for asking but i am still fairly confused on where to place in the method
Do i replace this code


with this code



 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. Inside your main method before you read an integer from your input (line 10) check if it has an integer at all (using integer.hasNextInt()).
 
Roy Poh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alright So i tried it like this


They tell me that integer is not a varible

 
Roy Poh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println("");
System.out.println("**************Expenditure**************");
System.out.println("1) Enter monthly expenses");
System.out.println("2) Display detailed expediture");
System.out.println("3) Quick glance at monthly expenses");
System.out.println("4) Exit");
System.out.print("Please select your choice{1-3}:");
choice=input.hasNextInt();
choice = input.nextInt();

I tried anothor way of doing it but they tell me that the value of choice should be a boleean how do i solve this
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do not assign input.hasNextInt() to choice. Method hasNextInt tells you whether an user provided something that is an integer. If the user did that, the method returns true and the program might proceed. If he/she didn't the method returns false and you should ask him/her to try again and the program should go to beginning of the loop (for example by using continue). Be aware that hasNextInt won't move Scanner's position so you need to do this by using input.next() (after you checked that an input was not integer).

Are you familiar with conditional statement?

What do you think a condition is in your case?
 
Roy Poh
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i tried what you said


They returned me the same error


so do I use a if else that does smthing like


or would a do while loop be better because i do want it to loop back if it is not an integer



which is better?Or is it even nneded?

 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would strongly suggest you StopCoding (<---click that). You seem to be trying random things, and not trying to UNDERSTAND what you are doing.

While this may let you end up with a working program, if someone asks you HOW it works, you'll be stuck. Further, this method of programming only works for relatively simple tasks.

Once you figure out what you need to do, only then should you start writing java.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Roy Poh wrote:Hi so my program works fine but there are some exceptions which i am not sure how to deal with, like how do i make it such that when the choice is not an integer but a character, it will loop back to the menu. I am new to java so it would be great if you could guide me

First: Follow Fred's advice. It's exactly what I would have said too.

Second: Understand that dealing with user input is finicky.
The keyboard is a character-based device, so even if you ask them for a number, there will always be some idiot who tries to enter letters or other characters instead. Oddly enough, such idiots are no bad thing, because they ensure that your program is "fool"-proof.

My technique:
1. Always get strings. I generally use nextLine(), because it forces the user to hit the 'return' key before my program processes the input.
2. Convert the String they typed in. If this fails, tell them they're an idiot and go back to Step 1.
3. Return the converted value.
However, there are many other possibilities.

You might also want to look at the Console class (java.io.Console) because it contains some nice methods that combine the business of taking input and displaying a prompt message. It's interface is more fluent as well.

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:You might also want to look at the Console class (java.io.Console) because it contains some nice methods that combine the business of taking input and displaying a prompt message.

If you choose to use Console class, be careful. Do not run your program with an IDE because you might get NullPointerException.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:If you choose to use Console class, be careful. Do not run your program with an IDE because you might get NullPointerException.

Very good point. I presume that's because it's a relatively new class (1.6); or is there any other reason, Pawel?

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's because some IDEs run their processes in the background and they might not have a console attached to them.
So System.concole() might return null.

There was a question about this on the Ranch.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:It's because some IDEs run their processes in the background and they might not have a console attached to them.
So System.concole() might return null.

Ah, right.

@Roy: In which case you may not be able to use it - a big pity, because it's a very nice and simple class - so you're stuck with the accursed Scanner.

Winston
 
Paweł Baczyński
Bartender
Posts: 2087
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:In which case you may not be able to use it - a big pity, because it's a very nice and simple class - so you're stuck with the accursed Scanner.

But OP is just learning Java, right? So he doesn't use any IDE, right? And he compiles and runs his program from console, right ?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pawel Pawlowicz wrote:But OP is just learning Java, right? So he doesn't use any IDE, right? And he compiles and runs his program from console, right ?

If they've been reading this forum for any time, they will. Unfortunately, on the Java course that I ended up doing for a diploma, they had us on Eclipse in week 2; despite all my cries of "no, no, no, this is wrong!!!".

Winston
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!