• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Keep on prompting user after invalid input

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I made a program, and I need to keep prompting for coordinates if the user enters a non-negative, invalid input. Meaning that if the user enters a number above 2. The problem that I am running into is I get ArrayOutOfBoundsException for the higher numbers. Is there a way to bypass all those errors and just ask the user to pick again? And have the new code run well with my already existing code? Do I just need to do something like what I did with the !keyboard.hasNextInt() but for integers?

Code:

 
Marshal
Posts: 76433
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is probably no better way at the moment to prompt for valid input than a loop like that, BUT, you are only using the loop once. So if I enter
Campbell Ritchie 123 Campbell
you will get 123 for the first number and an exception for the second number. So what are you going to do? Are you going to write the loop again? I hope not because you now have repeated code. Within a few weeks you will have hundreds of loops like that. What you want to do is search my posts for “utility class” and I hope you will find enough information to create a keyboard input class which you can keep for ever.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Christian Nehila wrote:So I made a program, and I need to keep prompting for coordinates if the user enters a non-negative, invalid input. Meaning that if the user enters a number above 2. The problem that I am running into is I get ArrayOutOfBoundsException for the higher numbers. Is there a way to bypass all those errors and just ask the user to pick again? And have the new code run well with my already existing code? Do I just need to do something like what I did with the !keyboard.hasNextInt() but for integers?


Not sure, but user input is always a fiddly process, so my suggestion would be to write something that you can re-use, and maybe add to later on.

And you can find some useful information on how to do that here.

HIH

Winston
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic