Two Problems Try catch and If
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!
Code java:
import java.util.Scanner;
class testcatch
{
public static void main ( String []args )
{
int number = 1;
String a = "";
int age = 0;
int count = 1;
Scanner userin = new Scanner(System.in);
Scanner scan2 = new Scanner(System.in);
while (count <= 10)
{
try
{
System.out.println(+number+". Please enter your age:");
age = userin.nextInt();
++number;
if (age == 4)
{
System.out.println("");
System.out.println("Your age is "+age+". Your still young!.");
System.out.println("");
}
else if (age == 5)
{
System.out.println("");
System.out.println("Your age is "+age+". You getting old!.");
System.out.println("");
}
else if (age == 6)
{
System.out.println("");
System.out.println("Your age is "+age+". You getting older!.");
System.out.println("");
}
else
{
System.out.println("");
System.out.println("ONLY AGES 4 - 5 AND 6");
System.out.println("");
System.out.println("");
--count;
--number;
}
} catch (Exception e) {
System.out.println("");
System.out.println("ONLY AGES 4 - 5 AND 6");
System.out.println("");
System.out.println("");
--count;
--number;
}
if (count == 10)
{
System.out.println("Do you want to check ten more ages?");
a = scan2.nextLine();
if (a.equalsIgnoreCase("yes") || a.equalsIgnoreCase("y"))
{
System.out.println("");
System.out.println("");
count=0;
number = 1;
}
else if (a.equalsIgnoreCase("No") || a.equalsIgnoreCase("n"))
{
System.out.println("");
System.out.println("");
System.out.println("Bye Bye!");
System.out.println("");
System.exit(0);
}
else
{
// How to return to yes or no question?
}
}
{count++;}
}
}
}
Re: Two Problems Try catch and If
What are you doing here? Why is this line surrounded in brackets?
Also, I'd check where you put that statement I quoted above, because at first glance it looks as if it's outside the while-loop's block of code.
As to answer you question about asking the yes-no-question again, think through the logic. While (hint, hint) the user doesn't input "yes" or "no", you want to ask again. Put that logic into your code.
Finally, I'd check the number of closing brackets (this character --> } ) you have; at a glance you may have too many. I'd just check again.
Hopefully this helps!
Re: Two Problems Try catch and If
Thanks for the reply,
I see what you mean about the curly brackets around the count. I removed them.
The program still acts the same, it compiles correctly but when the catch is triggered it freaks out and keeps looping the catch statement.
I think I have the right amount of brackets in there ( Very new to java though)
Also as you were hinting to use a while loop at the end for the yes or no question, I already have that section in the loop and tried this but it wouldn't work.
I'm probably way off here but ill post it anyway.
Code :
if (count == 10 || L == 25)
{
System.out.println("Do you want to check ten more ages?");
a = scan2.nextLine();
if (a.equalsIgnoreCase("yes") || a.equalsIgnoreCase("y"))
{
System.out.println("");
System.out.println("");
count=0;
number = 1;
}
else if (a.equalsIgnoreCase("No") || a.equalsIgnoreCase("n"))
{
System.out.println("");
System.out.println("");
System.out.println("Bye Bye!");
System.out.println("");
System.exit(0);
}
else
{
L=25;
// This wont return user to the question
}
} // I think this closes the nested if statement
count++;
} // This is the while loop close
}
}
Re: Two Problems Try catch and If
Quote:
I already have that section in the loop
You may have to put a loop in the loop that detects invalid responses is what I'm saying. Inception, with Java. :D
For me to offer assistance with the catch error, you need to be more specific... Maybe you could put extra println statements throughout the program to test what code it's reaching and how often.
Re: Two Problems Try catch and If
Quote:
First problem I have is with my try and catch. Its hard to explain (I don't know whats wrong with it)
Let's make it simple for you to explain. How does it behave and how do you want it to behave?
Quote:
My second problem is with the bottom section where I ask do you want to check ten more?
Set a flag and check it in your loop's condition.