Java Help Needed.......

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 3
Reputation: paydfody is an unknown quantity at this point 
Solved Threads: 0
paydfody paydfody is offline Offline
Newbie Poster

Java Help Needed.......

 
0
  #1
Oct 24th, 2007
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

}
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: kmlittle is an unknown quantity at this point 
Solved Threads: 0
kmlittle kmlittle is offline Offline
Newbie Poster

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

 
0
  #2
Oct 24th, 2007
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.

  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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: paydfody is an unknown quantity at this point 
Solved Threads: 0
paydfody paydfody is offline Offline
Newbie Poster

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

 
0
  #3
Oct 25th, 2007
Thanks for the suggestions & the response. I am writing in Java 2 SDK.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 6,143
Reputation: jwenting is just really nice jwenting is just really nice jwenting is just really nice jwenting is just really nice 
Solved Threads: 212
Team Colleague
jwenting's Avatar
jwenting jwenting is offline Offline
duckman

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

 
0
  #4
Oct 25th, 2007
System.exit(1) indicates abnormal program termination (crash, error).
Normal termination is indicated by exit code 0.
As people are clearly allowed to attack me but I'm not allowed to defend myself, I no longer post to this site.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: kmlittle is an unknown quantity at this point 
Solved Threads: 0
kmlittle kmlittle is offline Offline
Newbie Poster

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

 
0
  #5
Oct 25th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Java
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC