![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
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";
}
} |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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)
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: ')
|
|
|
|
|
|
#3 | |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
Re: Try and Catch Statements
Quote:
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!");
}
} |
|
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
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: ')
|
|
|
|
|
|
#5 |
|
Programmer
Join Date: Jan 2008
Posts: 54
Rep Power: 1
![]() |
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!");
}
} |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
Re: Try and Catch Statements
Java Syntax (Toggle Plain Text)
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)
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: ')
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|