944,117 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1047
  • Java RSS
Oct 24th, 2007
0

Java Help Needed.......

Expand Post »
This code is meant to be a simple quiz, but I am getting 4 errors that I cannot figure out. Thanks in advance.




import java.io.*;
public class Quiz
{
public static void main(String args[])
{
int correctCounter;
InputStreamReader reader;
BufferedReader buffer;
keyboard = new BufferedReader (new InputStreamReader (System.in));
}

String seekResponse;
{ System.out.println("Welcome to Dan's Quiz .. ");
{
// choose the subject
System.out.println("enter M for Math, S for Science or x for exit");
seekResponse = keyboard.readline();
if (seekResponse.equals ("M")) //Select the Math quiz
{
System.out.println("What is the sum of 120 & 120?"); //Math Question1
System.out.printIn("a. 0");
System.out.printIn("b. 240");
System.out.printIn("c. 220");
System.out.printIn("Press X to exit");
seekResponse = keyboard.readline();
if (seekResponse=("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("correct");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit; // do exit logic
System.out.println("What is the product of 12 & 12?"); //Math Question2 Line34
System.out.printIn("a. 144");
System.out.printIn("b. 24");
System.out.printIn("c. 1");
System.out.printIn("Press X to exit");
if (seekResponse.equals ("a"))
System.out.println("correct answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("incorrect answer");
else if (seekResponse.equals ("x"))
exit;// do exit logic
}
else if (seekResponse.equals ("S")); //Select the Science quiz
{
System.out.println("A snake is a member of which animal group?"); //Science Question1
seekResponse = keyboard.readline();
System.out.printIn("a. Mammal");
System.out.printIn("b. Amphibian");
System.out.printIn("c. Reptile");
if (seekResponse.equals ("a"))
System.out.println("incorrect answer");
if (seekResponse.equals ("b"))
System.out.println("incorrect answer");
if (seekResponse.equals ("c"))
System.out.println("correct");
}
{
else if (seekResponse.equals ("x"))
exit;// do exit logic

}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paydfody is offline Offline
3 posts
since Oct 2007
Oct 24th, 2007
0

Re: Java Help Needed.......

You should try and put your code into code blocks next time but here's what I see wrong:
  1. keyboard is not declared
  2. seekResponse.equals ("M") checks for an upper case letter same with seekResponse.equals ("s")
  3. if (seekResponse=("a")) should be if(seekResponse.equals("a"))
  4. exit; should be System.exit(1);

Here's what I did for this as an example for you and it is commented out so that you can see what I did in it.

Java Syntax (Toggle Plain Text)
  1. import java.io.*;
  2.  
  3. public class Test {
  4. public static void main(String[] args) throws IOException {
  5. //Create instances of the InputStream and BufferedReader
  6. InputStreamReader reader = new InputStreamReader(System.in);
  7. //Buffer the inputstream
  8. BufferedReader buffer = new BufferedReader(reader);
  9.  
  10. //Create the variables counter, seeker
  11. //counter is the number of correct answers incresed by one everytime a correct answer is supplied
  12. //seeker is used to store keyboard input to check for the type of quiz and answers
  13. int correctCounter = 0;
  14. String seeker;
  15.  
  16. //Say hello to the user and display an options menu
  17. System.out.println("Welcome to [Name] Quiz...");
  18. System.out.println("Enter 'M' for math, 'S' for scients, or 'X' to exit...");
  19.  
  20. //Reference the buffer.readLine() command to seeker to grab the input
  21. seeker = buffer.readLine();
  22.  
  23. //select the math quiz
  24. //Must use the equals method to compare strings == is invalid
  25. //seeker.equalsIgnoreCase(String) takes input regardless of lowercase and uppercase so m is equal to M
  26. if(seeker.equalsIgnoreCase("m"))
  27. {
  28. System.out.println("What is the sum of 5 + 5?");
  29. System.out.print("A. 10\n B. 0\n C. 225");
  30.  
  31. //reference the buffer.readLine() command to seeker to grab the input
  32. seeker = buffer.readLine();
  33.  
  34. //if 'a' is typed display correct answer
  35. if(seeker.equalsIgnoreCase("a"))
  36. {
  37. System.out.println("Correct Answer!");
  38. //Increase the number of correct answers the user has answered
  39. correctCounter += 1;
  40. }
  41. //if 'b' is typed display incorrect answer
  42. else if(seeker.equalsIgnoreCase("b"))
  43. {
  44. System.out.println("Incorrect Answer...");
  45. }
  46. //if 'c' is typed display incorrect answer
  47. else if(seeker.equalsIgnoreCase("c"))
  48. {
  49. System.out.println("Incorrect Answer...");
  50. }
  51. else
  52. //the user typed invalid input
  53. {
  54. System.out.println("Invalid input, try again.");
  55. //check for input again
  56. seeker = buffer.readLine();
  57. }
  58. }
  59. //exit the program
  60. //if the input is 'x' exit the program
  61. else if(seeker.equalsIgnoreCase("x"))
  62. {
  63. //ends the program nicely
  64. System.exit(1);
  65. }
  66. //Tell the user to try again if they type anything else
  67. else
  68. {
  69. System.out.println("Invalid Input! Please try again!");
  70. seeker = buffer.readLine();
  71. }
  72. }
  73. }

Also I would like to ask what type of environment your using to write java in.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kmlittle is offline Offline
3 posts
since Oct 2007
Oct 25th, 2007
0

Re: Java Help Needed.......

Thanks for the suggestions & the response. I am writing in Java 2 SDK.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
paydfody is offline Offline
3 posts
since Oct 2007
Oct 25th, 2007
0

Re: Java Help Needed.......

System.exit(1) indicates abnormal program termination (crash, error).
Normal termination is indicated by exit code 0.
Team Colleague
Reputation Points: 1658
Solved Threads: 331
duckman
jwenting is offline Offline
7,719 posts
since Nov 2004
Oct 25th, 2007
0

Re: Java Help Needed.......

Yeah, I usually use 0 but, for some reason when I tried to compile the program in eclipse it didn't respond 0 so I went ahead and used 1.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kmlittle is offline Offline
3 posts
since Oct 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: identifying a carriage return in string
Next Thread in Java Forum Timeline: Help with Exercise 7.2





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC