This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Two Problems Try catch and If Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Two Problems Try catch and If " Watch "Two Problems Try catch and If " New topic
Author

Two Problems Try catch and If

Karl Collins
Greenhorn

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

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3432
    
  12
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 ?


Joanne
Karl Collins
Greenhorn

Joined: Nov 22, 2011
Posts: 21
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.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Claudiu Chelemen
Ranch Hand

Joined: Mar 25, 2011
Posts: 71

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
Randall Twede
Ranch Hand

Joined: Oct 21, 2000
Posts: 4340
    
    2

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


SCJP
Visit my download page
Karl Collins
Greenhorn

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

Joined: Nov 22, 2011
Posts: 21
Edit: I solved most of this problem thanks for the help for now ranchers!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Two Problems Try catch and If
 
Similar Threads
Java Code Generator
Do while error, I'm going crazy
Method Creation and debugging.
getting java.lang.NoSuchMethodError: main error! thanks =)
Looping problem with ints