This week's book giveaway is in the HTML, CSS and JavaScript forum.
We're giving away four copies of hapi.js in Action and have Matt Harrison on-line!
See this thread for details.
Win a copy of hapi.js in Action this week in the HTML, CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Looping

 
chop vick
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?!!\


 
Junilu Lacar
Sheriff
Posts: 9338
96
Android Eclipse IDE IntelliJ IDE Java Linux Mac Scala 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?

 
Dave Tolls
Ranch Hand
Posts: 2207
20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a loop in that latest code?
 
Campbell Ritchie
Sheriff
Posts: 52001
90
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!