• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

Try catch with loop

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For some reason, I keep falling into an infinite loop. Here is the code up to the end of the loop. Where did I go wrong?

 
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

What makes you think you're falling into an infinite loop? If anything, it looks like your loop will always exit after the first time through.
 
Carey Brown
Saloon Keeper
Posts: 7179
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to detect and flush anything in the input stream that is not a double.

 
Lo Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:what input are you giving it?

When I give it normal integers it works fine. When I test my "try-catch" by entering a letter is when it just goes
 
Lo Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Welcome to the Ranch!

What makes you think you're falling into an infinite loop? If anything, it looks like your loop will always exit after the first time through.

That what I thought would happen but when I hit enter it just runs the loop saying "Enter valid number" until I hit the red stop button on eclipse.
 
Junilu Lacar
Marshal
Posts: 15638
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Lo Jones wrote:but when I hit enter it just runs the loop saying "Enter valid number" until I hit the red stop button on eclipse.


None of the code you posted would give that particular message. Is that the exact message or are you just paraphrasing? Because if it's the exact message you're seeing, then it's caused by some other code that you haven't shared. If you're paraphrasing, don't. Programming is a precise art: "Yes" is different from "YES" and definite not "sure, that's what I meant."
 
Lo Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:

Lo Jones wrote:but when I hit enter it just runs the loop saying "Enter valid number" until I hit the red stop button on eclipse.


None of the code you posted would give that particular message. Is that the exact message or are you just paraphrasing? Because if it's the exact message you're seeing, then it's caused by some other code that you haven't shared. If you're paraphrasing, don't. Programming is a precise art: "Yes" is different from "YES" and definite not "sure, that's what I meant."

says "Please enter valid height"
 
Lo Jones
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:Did you see my post about flushing the input?

that worked!😊 I've been messing with it trying to figure it out. no luck though lol...is it bad protocol to ask a follow on question?
 
Marshal
Posts: 69493
277
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please show us the latest version of your code, which actually works.
Don't use numbers when you want booleans.Note:-
  • 1: No change to your reading code yet.
  • 2: Line 25 requires the height be in a certain range.
  • 3: Line 30 will set needsInput if an exception is thrown anywhere.
  • 4: The solution Carey showed is a much better way to handle Scanner input than looking for exceptions. I learnt such a technique from Rob Spoor a long time ago. You must use next() to get it to work.
  • The following is therefore a much better loop:-Lines 25‑26 don't have to go in any particular order.
    I shall leave you to work out what happens if minHeight > maxHeight(‍), and how to print a request for repeated input if the input is in the wrong range. If you do the same for width, you will find out that you need a utility method.. Many of us olld hands have utility classes for that sort of keyboard input.
     
    Lo Jones
    Greenhorn
    Posts: 7
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Carey Brown wrote:Ask away.



    That worked when dealing with inputting something other than an integer. Now I am trying to make it where the int > 0. Because you can't have a negative height.
     
    Carey Brown
    Saloon Keeper
    Posts: 7179
    65
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The code says nextDouble(), so it's looking for either an integer or a floating point number. To check if it is >0 change the expression in the while().
     
    Are we home yet? Wait, did we forget the tiny ad?
    Devious Experiments for a Truly Passive Greenhouse!
    https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
      Bookmark Topic Watch Topic
    • New Topic