View Single Post
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