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


}

Recommended Answers

All 4 Replies

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.

import java.io.*;

public class Test {
	public static void main(String[] args) throws IOException {
		//Create instances of the InputStream and BufferedReader
		InputStreamReader reader = new InputStreamReader(System.in);
		//Buffer the inputstream
		BufferedReader buffer = new BufferedReader(reader);
		
		//Create the variables counter, seeker
		//counter is the number of correct answers incresed by one everytime a correct answer is supplied
		//seeker is used to store keyboard input to check for the type of quiz and answers
		int correctCounter = 0;
		String seeker;
		
		//Say hello to the user and display an options menu
		System.out.println("Welcome to [Name] Quiz...");
		System.out.println("Enter 'M' for math, 'S' for scients, or 'X' to exit...");
		
		//Reference the buffer.readLine() command to seeker to grab the input
		seeker = buffer.readLine();
		
		//select the math quiz
		//Must use the equals method to compare strings == is invalid
		//seeker.equalsIgnoreCase(String) takes input regardless of lowercase and uppercase so m is equal to M
		if(seeker.equalsIgnoreCase("m")) 
		{
			System.out.println("What is the sum of 5 + 5?");
			System.out.print("A. 10\n B. 0\n C. 225");
			
			//reference the buffer.readLine() command to seeker to grab the input
			seeker = buffer.readLine();
			
			//if 'a' is typed display correct answer
			if(seeker.equalsIgnoreCase("a")) 
			{
				System.out.println("Correct Answer!");
				//Increase the number of correct answers the user has answered
				correctCounter += 1;
			} 
			//if 'b' is typed display incorrect answer
			else if(seeker.equalsIgnoreCase("b")) 
			{
				System.out.println("Incorrect Answer...");
			}
			//if 'c' is typed display incorrect answer
			else if(seeker.equalsIgnoreCase("c"))
			{
				System.out.println("Incorrect Answer...");
			}
			else 
			//the user typed invalid input
			{
				System.out.println("Invalid input, try again.");
				//check for input again
				seeker = buffer.readLine();
			}
		} 
		//exit the program
		//if the input is 'x' exit the program
		else if(seeker.equalsIgnoreCase("x")) 
		{
			//ends the program nicely
			System.exit(1);
		}
		//Tell the user to try again if they type anything else
		else
		{
			System.out.println("Invalid Input! Please try again!");
			seeker = buffer.readLine();
		}
	}
}

Also I would like to ask what type of environment your using to write java in.

Thanks for the suggestions & the response. I am writing in Java 2 SDK.

System.exit(1) indicates abnormal program termination (crash, error).
Normal termination is indicated by exit code 0.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.