File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Two Problems Try catch and If

 
Karl Collins
Greenhorn
Posts: 21
  • 0
  • Mark post as helpful
  • send pies
  • 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!

 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 1
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Claudiu Chelemen
Ranch Hand
Posts: 75
Eclipse IDE Java Oracle
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic