• Post Reply Bookmark Topic Watch Topic
  • New Topic

Error handling - clear data, have user try again.  RSS feed

 
Will Pritchard
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello forum! My name is Will and I'm super new to Java. I'm trying to create a program that won't let a user enter in a letter or double value in a do-while loop that is focused on gathering integer data. I want to make it so if an error occurs, there will be a catch, clear, and then have the user retry entering in data. How do I setup a try-catch statement that will stop errors, clear them, and have the user enter in new data?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What kind of errors are you talking about? If you mean data entry errors then those are not the type that you'd handle with exceptions, but with normal program control flow.
 
Campbell Ritchie
Marshal
Posts: 56570
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Have you studied the methods of the well‑known Scanner class? If you use them correctly you can avoid using Exceptions at all.
 
Will Pritchard
Greenhorn
Posts: 7
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:What kind of errors are you talking about? If you mean data entry errors then those are not the type that you'd handle with exceptions, but with normal program control flow.
Basically, integer input errors. Let's say the method asks for an integer such as, "Type in a number: " and the user accidentally hits, "5t." An error would then throw and then clear the data to have the user try again.

Campbell Ritchie wrote:Welcome to the Ranch! Have you studied the methods of the well‑known Scanner class? If you use them correctly you can avoid using Exceptions at all.


I have not actually. Thanks for that info! I'll see what I can use.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will Pritchard wrote:Basically, integer input errors. Let's say the method asks for an integer such as, "Type in a number: " and the user accidentally hits, "5t." An error would then throw and then clear the data to have the user try again...

In addition to Campbell's suggestion, you might want to have a look at the UserInput page. Getting input from a user is not easy - as I suspect you're finding out - and there are a lot of things that can go wrong.

I should add that it shows you one way to do it, and there are are many others; but hopefully it gives you something to think about.

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