• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to stop the loop from ending when wrong input is entered  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys,

got a menu system going here



in the line:
while (userInput.hasNextInt()) {

i understand that its checking if an integer was entered
the menu is working fine

but if i type anything else but n integer the program ends...how can i make it so it says "Not a valid selection" but the loop resumes?

thanks
 
Yosuf Ibrahim
Ranch Hand
Posts: 128
4
Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
add line 18 in a try catch loop where if the user entered anything that is not a number.

If you do not know what a try-catch is. Basically, you are telling the computer to test this code and if it catches this kind of exception to do that. There are loads of exceptions but the one you are having I think is "InputMismatchException"

 
Vinod Khatrii
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to achieve same is keep on reading in an infinite loop and break out of the loop only when 4 is entered.

In the beginning we check whether valid integer is entered. If not then print the error message. Please find the code for same below:

 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yosuf Ibrahim wrote:add line 18 in a try catch loop where if the user entered anything that is not a number. . . .
Afraid that won't help the original poster. There is no chance of that line throwing an Exception, so a try‑catch would be pointless.
The solution with return in line 36 would work, but I don't like breaking out of a while (true) ... if I can help it. Let's go back to your original code:-Only  lines 12 and 36 have been changed. Now entering something which isn't an int will terminate the loop. If that isn't what you want, consider that there is a kind of loop where you can start the loop and only repeat the loop if a certain condition is fulfilled. Have a look at the kinds of loop in the Java™ Tutorials. You will probably want the loop to run as long as the option is not equal to 4.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ha ha campbell genius...look im going along with that solution its fine for now
letter keys now exit the program

just need some advice regarding this...im sure you can see what im trying to do
im busy trying to make the methods that will add delete and list companys in the arraylist
should the arraylist be initialized in the constructor? or in the main method? or somewhere else?
i understand a lot about the basics,its putting it all together where im having trouble.

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
also this design is flawed isnt it?
my main menu shouldnt be in a method should it?
as when i try call the addCompany method from my object in the mainMenu method it doesnt see it.
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . . methods that will add delete and list companys in the arraylist
should the arraylist be initialized in the constructor?
You haven't said anything about lists. So that is a hard question to answer. Most classes with fields shou‍ld have those fields instantiated in the constructor, I think, but we can't be definite without knowing more details. One of those details would be how many classes you have.
or in the main method? . . .
That one is easy to answer. No. Nono. Nonononononononononononono.

That sort of question depends on how you have designed your app.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
jon ninpoja wrote:. . . methods that will add delete and list companys in the arraylist
should the arraylist be initialized in the constructor?
You haven't said anything about lists. So that is a hard question to answer. Most classes with fields shou‍ld have those fields instantiated in the constructor, I think, but we can't be definite without knowing more details. One of those details would be how many classes you have.
or in the main method? . . .
That one is easy to answer. No. Nono. Nonononononononononononono.

That sort of question depends on how you have designed your app.


i only have two classes

user (which is just main,and creates an instance of Company

ok never in main...

the list method seems to work

but i cant add to the arraylist as the switch case in the mainMenu method cant see any methods in the company object
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but line 29 wont let me go...myCompany.addCompany(); as it cant see methods associated with the object because of local scope...how would i get around this?

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry no...thats not what i meant to say...i dont think this is to do with local scope
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:but line 29 wont let me go...myCompany.addCompany(); as it cant see methods associated with the object because of local scope...how would i get around this?


Well, since you never showed us any code that has a reference named myCompany, or any class with a method named addCompany(), we really don't have any context on the issue / question.

Henry
 
Campbell Ritchie
Marshal
Posts: 56584
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks like code in an early stage of development. You would gradually replace the print statements with calls to methods to do whatever the print would have said. As Henry said, there is nothing shown for us to comment on. You would probably not however want to do the adding directly inside the switch statement.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!