• Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping  RSS feed

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
New to java and having a problem getting my loop to work. program runs and stops the user from inputting anything less than zero but not stopping them from inputting anything over 1000.

Also having a problem with the NumberFormatException as i can't let user input letters either!

Once loop is working i then need to read user input in JOptionPane and pass it to a constructor in a different class.

First time on this so thanks for any help given!!

 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
changed my code a bit and program now stops user from inputting anything over 1000. But how do i get it to stop user from inputting letters or words?!!\


 
Sheriff
Posts: 11625
187
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand when the catch (NumberFormatException e) on line 36 will be executed?  Do you know how to use loops?

See Java Tutorial - Exceptions
See Java Tutorial - Loops
 
chop vick
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so have came up with this now but loop finishes after user is prompted to enter new number after first input is invalid. what to do?

 
Rancher
Posts: 3250
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a loop in that latest code?
 
Marshal
Posts: 57276
175
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

I can see a problem, I am afraid. You have got a method inside a display component which isn't part of the display. Testing whether the input is a number or not is part of your “business logic” and ought to be in a “business class”. Which isn't the opposite of economy class. You shou‍ld have a method which tests whether the input is a number or not; the display class can take action, e.g. remove the entry from the text field.
There are better ways to test whether a String represents a number than catching Exceptions. You will find some suggestions in this thread from last Summer.
 
The moth suit and wings road is much more exciting than taxes. Or this tiny ad:
ScroogeXHTML 7.1 - RTF to HTML5 / XHTML converter
https://coderanch.com/t/690611/ScroogeXHTML-RTF-HTML-XHTML-converter
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!