File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Looping problem with ints Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Looping problem with ints" Watch "Looping problem with ints" New topic
Author

Looping problem with ints

Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
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

Joined: Sep 08, 2007
Posts: 3071
    
  33

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.


Mohamed Sanaulla | My Blog
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 07, 2011
Posts: 29
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Aug 07, 2011
Posts: 29
that's what i get for not paying attention thanks for clearing that up.
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
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!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Looping problem with ints