• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Devaka Cooray
Saloon Keepers:
  • Ganesh Patekar
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • salvin francis
Bartenders:
  • Ron McLeod
  • Frits Walraven
  • Pete Letkeman

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: 12530
206
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?

 
Master Rancher
Posts: 3676
40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see a loop in that latest code?
 
Marshal
Posts: 60860
190
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!