• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Looping

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't see a loop in that latest code?
 
Marshal
Posts: 80110
414
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic