Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Two Problems Try catch and If

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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!

     
    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++;}
     
    }
     
    }
    }
    Last edited by Callcollect; December 18th, 2011 at 09:02 PM.


  2. #2
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Two Problems Try catch and If

    {count++;}
    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!
    Last edited by snowguy13; December 18th, 2011 at 08:10 PM.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    8
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.

    		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
     
    }
    }

  4. #4
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: Two Problems Try catch and If

    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.

    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.
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Two Problems Try catch and If

    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?

    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.

Similar Threads

  1. how do i try/catch this?
    By safra in forum Exceptions
    Replies: 6
    Last Post: October 29th, 2011, 12:14 PM
  2. Try, catch? array help
    By alkamenes in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 15th, 2011, 12:17 AM
  3. [SOLVED] Problems with Try/Catch Catching Wrong Exception
    By bgroenks96 in forum What's Wrong With My Code?
    Replies: 14
    Last Post: June 10th, 2011, 09:08 PM
  4. catch all
    By silverspoon34 in forum Exceptions
    Replies: 1
    Last Post: November 29th, 2009, 02:18 PM
  5. try/catch
    By rsala004 in forum Exceptions
    Replies: 5
    Last Post: July 19th, 2009, 04:20 PM