File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Looping problem with ints

 
Karl Collins
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey

So I initially thought I had fixed this problem but it turns out I didn't.
Below is two programs I wrote Agetesting (A program to test the users age) and Sizetest (a program to test the users size)
The program 'size test' mainly uses strings and 'age testing' uses mainly int's. I think the int's may be where my problem is. I really have no clue as I am a complete beginner and this problem is really hindering my learning.

What happens in the NOT working program (age testing) is when the Try/Catch is triggered the program constantly repeats the phrase "Only enter 1 - 100 only" over and over

In comparison to the working program (Size test) when the Try/Catch is triggered in this program the program simply says one time "Please only enter Small, Big or Bigger" then asks the user the main question again.

Below are both programs, please if anyone can help I'd be grateful! Thanks.

-NOT WORKING PROGRAM AGE TEST -



- WORKING PROGRAM SIZE TEST -




 
Mohamed Sanaulla
Saloon Keeper
Pie
Posts: 3159
33
Google App Engine Java Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your AgeTesting program you ask the user to enter 1-100



But you dont seem to be taking the user input.

Also the "loop" variable used for the while loop isn't being incremented, if you dont increment it then your while loop would go into infinite loop. Same goes with the SizeTest program where variable "i" is not being incremented.
 
Karl Collins
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mohamed Sanaulla wrote:In your AgeTesting program you ask the user to enter 1-100



But you dont seem to be taking the user input.

Also the "loop" variable used for the while loop isn't being incremented, if you dont increment it then your while loop would go into infinite loop. Same goes with the SizeTest program where variable "i" is not being incremented.


Hey, thanks for the reply.

This line


is the exact same as the line in the "size test" program but yet it works in size test and not agetest



also I understand there is no increment although I don't mind the infinite loop as long as it asks for user input each time. My problem is on "Age testing" if Try/Catch is triggered the "Only enter 1 to 100" line is looped and no user input is asked unlike the "Size Test" program.

 
John Jai
Rancher
Posts: 1776
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you still need it to behave as an infinite loop, then you can move the scanner using the next() method in the catch block.


1. Use camel casing for naming class - AgeTesting and not agetesting
2. The catch block has similar statements to the main else block - modify them.
3. Check if really the String version gives an Exception or only the main else block is executed each time. Or check which method throws an Exception and when - http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt() and http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine()
 
Daniel Vlad
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hmm that's weird. i just copied your agetesting class and ran it, it works just fine for me even if you enter an age beyond 100. So i'm guessing it's something else?
 
John Jai
Rancher
Posts: 1776
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Vlad wrote:hmm that's weird. i just copied your agetesting class and ran it, it works just fine for me even if you enter an age beyond 100. So i'm guessing it's something else?

It's when the catch block is executed by giving a non-integer input.
 
Daniel Vlad
Greenhorn
Posts: 29
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that's what i get for not paying attention thanks for clearing that up.
 
Karl Collins
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Jai wrote:If you still need it to behave as an infinite loop, then you can move the scanner using the next() method in the catch block.


1. Use camel casing for naming class - AgeTesting and not agetesting
2. The catch block has similar statements to the main else block - modify them.
3. Check if really the String version gives an Exception or only the main else block is executed each time. Or check which method throws an Exception and when - http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextInt() and http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Scanner.html#nextLine()



Thanks John that's fixed my problem, and thanks for the tips also. I have been reading on the scanner and putting it into practice! That page you linked to is really helpful!
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic