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

Looping problem with ints

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 -




 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Rancher
Posts: 1776
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's what i get for not paying attention thanks for clearing that up.
 
Karl Collins
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
reply
    Bookmark Topic Watch Topic
  • New Topic