• 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:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
Bartenders:

Two Problems Try catch and If

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I have 2 problems if anyone can offer help i'd really appreciate it!

First problem I have is with my try and catch. Its hard to explain (I don't know whats wrong with it)

My second problem is with the bottom section where I ask do you want to check ten more? I want to return to the question when the else is triggered. I don't even know if this is possible.

Thanks for looking!

 
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Karl Collins wrote:First problem I have is with my try and catch. Its hard to explain (I don't know whats wrong with it)


What makes you think there is anything wrong with it ? Is it doing something when you think it shouldn't ? Is it not doing something that you think it should ?
 
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

Joanne Neal wrote:

Karl Collins wrote:First problem I have is with my try and catch. Its hard to explain (I don't know whats wrong with it)


What makes you think there is anything wrong with it ? Is it doing something when you think it shouldn't ? Is it not doing something that you think it should ?



Thanks for the reply,

Well when the try & catch triggers it seems to keep repeating the catch. Over and Over.

For example the program says

"2. Enter your age:"

And you type "apple"

the program says "ONLY AGES 4 - 5 AND 6" over and over.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's because the try-catch is inside the while loop, so after the code in the catch block is done, it's just going through the loop again.

Another problem is that the condition of the while loop is count <= 10 but you are not incrementing count anywhere (you're only decrementing it in a couple of places) so it's going to take very long for count to become 10.
 
Ranch Hand
Posts: 75
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may also try, inside the catch Exception block, to call userin.next();
That would help clearing out the invalid input you previously inserted.

Claudiu
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is some very confusing code for me.. i will keep trying to figure it out

I don't even know if this is possible.


i am certain it is but i am still trying to figure out your code.
i give up i cant tell what you are trying to do
 
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
Ok I am sorry for the shitty explanation. First off I tried to put the catch outside of the loop but it doesn't work.

What I want is to ask "What age are you" ten times. I want only 4, 5 and 6 to be able to be entered and anything else to be refused. All other numbers entered will be handled by my else statement but whenever a character is entered the program receives an error and quits. So I used the Try/Catch. But for some reason when the catch is triggered it loops over and over again. I only want the try/catch to do the same thing my else does.

Ill worry about the second bit when I fix this first bit.

I hope you can understand now.

Thanks or the replys

Oh and I don't fully understand what Claudia was saying although maybe she is right? Sorry for the ignorance.
 
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
Edit: I solved most of this problem thanks for the help for now ranchers!!
 
How do they get the deer to cross at the signs? Or to read this tiny ad?
Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders
https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing
reply
    Bookmark Topic Watch Topic
  • New Topic