Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 28th, 2008, 9:51 PM   #1
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Try and Catch Statements

Ok, I have this code and I am trying to put try and catch statements in it and I am unable to. I receive a compiler error that says can't have try without catch, and catch without try.

How can I do this?

import java.io.*;

class ModifiedTest
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        String strInput;

        System.out.println("Do you like Rock Music?:");
        strInput = dataIn.readLine();  //reads data from console

try
{
        
	if ("y".equals(strInput))
        {
            System.out.println("Do you like Marilyn Manson?:");
            strInput = dataIn.readLine();  //reads data from console
            if ("y".equals(strInput))
            {
                System.out.println("You are a hardcore Rocker");
            }
            else if ("n".equals(strInput))
            {
                System.out.println("How could you not like Marilyn Manson?");
            }
            else
            {
                System.out.println("Illegal input: " + strInput);
            }
        }
        else if ("n".equals(strInput))
        {
            System.out.println("Ok, bye rock hater!");
        }
        
	else
	catch (TheException e)
        {
            System.out.println("" + e + " input: " + strInput);
        }

        System.out.println();
        System.out.println("Program Ending!");
	}
    }
}
class TheException extends Exception
{
	public String toString()
	{
		return "Illegal";
	}
}
kewlgeye is offline   Reply With Quote
Old Apr 29th, 2008, 1:51 AM   #2
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Try and Catch Statements

The reason behind this is bracket placement, what you have actually done in yurs is place the catch statement within the try statement
Java Syntax (Toggle Plain Text)
  1. try
  2. {
  3.  
  4. if ("y".equals(strInput))
  5. {
  6. System.out.println("Do you like Marilyn Manson?:");
  7. strInput = dataIn.readLine(); //reads data from console
  8. if ("y".equals(strInput))
  9. {
  10. System.out.println("You are a hardcore Rocker");
  11. }
  12. else if ("n".equals(strInput))
  13. {
  14. System.out.println("How could you not like Marilyn Manson?");
  15. }
  16. else
  17. {
  18. System.out.println("Illegal input: " + strInput);
  19. }
  20. }
  21. else if ("n".equals(strInput))
  22. {
  23. System.out.println("Ok, bye rock hater!");
  24. }
  25. }
  26. catch (TheException e)
  27. {
  28. System.out.println("" + e + " input: " + strInput);
  29. }
  30.  
  31. System.out.println();
  32. System.out.println("Program Ending!");
  33. }
  34. }

Note i have also removed the else statement. Now you will be given a different error converning the fact that TheException is never thrown in the try statement, this i'll let you look into.

If you do still need help on this just ask, but as a clue what exception are you trying to catch? and what throws it?


Cheers,
Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Apr 29th, 2008, 8:17 AM   #3
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Re: Try and Catch Statements

Quote:
Originally Posted by Freaky Chris View Post
The reason behind this is bracket placement, what you have actually done in yurs is place the catch statement within the try statement

Note i have also removed the else statement.
If you do still need help on this just ask, but as a clue what exception are you trying to catch? and what throws it?

Cheers,
Chris

I never got to see the error because I figured out rather quickly with you clue. Thanks, I wasn't throwing an exception, I never had the statement. I put it in their and now I am receiving the illegal input error correctly. Now I just have to modify my questions and call the exception for every question. Thanks again. This was the code after I modified it with your help.

import java.io.*;

class ModifiedTest_Tester
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        String strInput;

        System.out.println("Do you like Rock Music?:");
        strInput = dataIn.readLine();  //reads data from console
try
{
        
	if ("y".equals(strInput))
        {
            System.out.println("Do you like Marilyn Manson?:");
            strInput = dataIn.readLine();  //reads data from console
            if ("y".equals(strInput))
            {
                System.out.println("You are a hardcore Rocker");
            }
            else if ("n".equals(strInput))
            {
                System.out.println("How could you not like Marilyn Manson?");
            }
            else
            {
        		throw new TheException(); //calls
            }
        }
        else if ("n".equals(strInput))
        {
            System.out.println("Ok, bye rock hater!");
        }
}
	catch (TheException e)
        {
            System.out.println("Illegal input caught");
        }

        System.out.println();
        System.out.println("Program Ending!");
	}
    }
kewlgeye is offline   Reply With Quote
Old Apr 29th, 2008, 11:01 AM   #4
kewlgeye
Programmer
 
Join Date: Jan 2008
Posts: 53
Rep Power: 1 kewlgeye is on a distinguished road
Re: Try and Catch Statements

Ok, I have modified it more, and now I am going crazy because I can't see what I am doing wrong. I am sure that it is a bracket or something somewhere, but Ihave been here for 3 hours and I am not getting anywhere. This is what it is suppose to do.

ask if you like cookies
if yes --> do you like chocolate if yes --> you must like oreo cookies then. if no --> you must like peanut butter. end this section and continue with do you like milk, otherwise if you don't like cookies ---> do you like milk if yes ---> do you like chocolate milk if yes --> me and you both pal. if no --> I don't understand you, goodbye.

it compiles, but when I get to the do you like milk section, it doesn't allow me to answer it, and says me and you too pal. when asks me if i like oreo cookies, if I say no it says ok, so you don't like milk.

Any ideas?

import java.io.*;

class ModifiedTest_Tester
{
    public static void main(String args[]) throws IOException
    {
        BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
        String strInput;
	String strInput1;

        System.out.println("Do you like cookies?:");
        strInput = dataIn.readLine();  //reads data from console
try
{
        
	if ("y".equals(strInput))
        {
            System.out.println("Do you like chocolate?:");
            strInput = dataIn.readLine();  //reads data from console
	    if ("y".equals(strInput))
            {
                System.out.println("You must like Oreo cookies then");
            }
            else if ("n".equals(strInput))
            {
                System.out.println("If you don't like Chocolate, you must like peanut butter");
            }
            else
            {
        		throw new TheException(); //calls
            }
        }
      
        if ("n".equals(strInput))
	System.out.println("Do you like Milk?:");
	strInput1 = dataIn.readLine();  //reads data from console
	
	if ("y".equals(strInput1))
	{
	    System.out.println("Do you like Chocolate Milk?");
            if ("y".equals(strInput1))
            {
		System.out.println("Me and you both Pal, put er' their!!");
	    }
            else if ("n".equals(strInput1))
            {
                System.out.println("I don't Understand you, goodbye!!");
            }
            else
            {
        		throw new TheException(); //calls
            }
        }
	else if ("n".equals(strInput1))
        {
            System.out.println("Ok, so you don't like milk, your too picky!!!");
        }

		else
		{
		throw new TheException(); //calls
            	}
}
	catch (TheException e)
        {
            System.out.println("Illegal input caught, please enter a Y or N");
        }

        System.out.println();
        System.out.println("Program Ending!");
	}
    }
kewlgeye is offline   Reply With Quote
Old Apr 29th, 2008, 9:23 AM   #5
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Try and Catch Statements

Indeed glad you got it.

Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Old Apr 29th, 2008, 11:37 AM   #6
Freaky Chris
Hobbyist Programmer
 
Freaky Chris's Avatar
 
Join Date: Dec 2007
Location: England
Posts: 169
Rep Power: 1 Freaky Chris is on a distinguished road
Send a message via MSN to Freaky Chris
Re: Try and Catch Statements

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. class Main
  4. {
  5. public static void main(String args[]) throws IOException
  6. {
  7. BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));
  8. String strInput;
  9. String strInput1;
  10.  
  11. System.out.println("Do you like cookies?:");
  12. strInput = dataIn.readLine(); //reads data from console
  13. try
  14. {
  15.  
  16. if ("y".equals(strInput))
  17. {
  18. System.out.println("Do you like chocolate?:");
  19. strInput = dataIn.readLine(); //reads data from console
  20. if ("y".equals(strInput))
  21. {
  22. System.out.println("You must like Oreo cookies then");
  23. }
  24. else if ("n".equals(strInput))
  25. {
  26. System.out.println("If you don't like Chocolate, you must like peanut butter");
  27. }
  28. else
  29. {
  30. throw new TheException(); //calls
  31. }
  32. }
  33.  
  34. System.out.println("Do you like Milk?:");
  35. strInput1 = dataIn.readLine(); //reads data from console
  36.  
  37. if ("y".equals(strInput1))
  38. {
  39. System.out.println("Do you like Chocolate Milk?");
  40. strInput1 = dataIn.readLine(); //reads data from console
  41. if ("y".equals(strInput1))
  42. {
  43. System.out.println("Me and you both Pal, put er' their!!");
  44. }
  45. else if ("n".equals(strInput1))
  46. {
  47. System.out.println("I don't Understand you, goodbye!!");
  48. }
  49. else
  50. {
  51. throw new TheException(); //calls
  52. }
  53. }
  54. else if ("n".equals(strInput1))
  55. {
  56. System.out.println("Ok, so you don't like milk, your too picky!!!");
  57. }
  58.  
  59. else
  60. {
  61. throw new TheException(); //calls
  62. }
  63. }
  64. catch (TheException e)
  65. {
  66. System.out.println("Illegal input caught, please enter a Y or N");
  67. }
  68.  
  69. System.out.println();
  70. System.out.println("Program Ending!");
  71. }
  72. }
  73. class TheException extends Exception
  74. {
  75. public String toString()
  76. {
  77. return "Illegal";
  78. }
  79. }
I think that should work fine for you, i havn't fully tested but.

You had an if statement that was checking if the input was = to "n", and if it was then it was printing "Do you like milk?", otherwise it was skipping this line.

Java Syntax (Toggle Plain Text)
  1. if ("n".equals(strInput))
  2. System.out.println("Do you like Milk?:");
  3. strInput1 = dataIn.readLine(); //reads data from console

Also once you had asked whether or not they like milk, you then printed do you like chocolate milk and forgot to ask the user again, and so the last input is used as a check.

Cheers,
Chris
__________________
Who said i couldn't program
sarcasm = raw_input('Type in a sarcastic remark: ')
print sarcasm
Freaky Chris is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:59 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC