Click here to Skip to main content
15,664,823 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm a beginner in Java. I try to do a Quiz System program but keep getting error: cannot find symbol. The IDE pointing these error at obj.nextLine(). Here's the code:

Java
import java.util.Scanner;
import java.util.*;

class Student
{
    Scanner name1 = new Scanner(System.in);
    
    public void name()
       {
            System.out.println("What is your name?:");
            String name = name1.nextLine();
       
            System.out.println(name + " the little Lamb");
            System.out.println("Hi " + name + " the little Lamb");
        }
}

class Quiz extends Student
{
    public static void main(String[] arg)
    {
        Quiz obj = new Quiz();
        obj.name();
    }
}

class Question1 extends Quiz
{
    public void Question1()
    {
        System.out.println("Q1.What is the language used in this OOP course?");
        String language = obj.nextLine();
        
        switch(language)
        {
            case "Java":
                 System.out.println("Correct!!");
                break;
             default:
            System.out.println("Wrong! Please answer again");
        }
    }
}

class Question2 extends Question1
{
    public void Question2()
    {
        System.out.println("Q2.What is the command to print an output?");
        String output = obj.nextLine();
        
        switch(output)
        {
            case "System.out.println":
                 System.out.println("Correct!!");
                break;
             default:
            System.out.println("Wrong! Please answer again");
        }
    }
}

class Question3 extends Question2
{
    public void Question3()
    {
        System.out.println("Q3. How to compile Test.java using command prompt?");
        String compile = obj.nextLine();
        
        switch(compile)
        {
            case "javac Test.java":
                 System.out.println("Correct!!");
                break;
             default:
            System.out.println("Wrong! Please answer again");
        }
    }
}

class Question4 extends Question3
{
    public void Question4()
    {
        System.out.println("Q4. How to run Test.java using command prompt?");
        String run= obj.nextLine();
        
        switch(run)
        {
            case "java Test":
                 System.out.println("Correct!!");
                break;
             default:
            System.out.println("Wrong! Please answer again");
        }
    }
}

class Question5 extends Question4
{
   public static void main(String args[])
   {  
        System.out.println("Q5. Which of the package contains mathematical functions?\n" +
            "A. import java.lang.* " + "  |  " + "B. import java.awt.*\n" +
            "C. import java.util.Scanner;"  + "  |  "  + "D. import java.util.ArrayList\n");
        String Question5 = obj.nextLine();
    
        switch(Question5)
         {
            case "A":
                 System.out.println("Correct!!");
                 break;
            default:
                System.out.println("Wrong! Please answer again");
        }
    }
}

class Question6 extends Question5
{
     public static void main(String args[])
    { 
         System.out.println("Q6. Where does a java program start executing instructions from?\n" +
            "A. class " + "  |  " + "B. source file" + "C. main method"  + "  |  "  + "D. object\n");
         String Question6 = obj.nextLine();
         
        switch(Question6)
         {
            case "C":
                System.out.println("Correct!!");
                break;
             default:
                 System.out.println("Wrong! Please answer again");
        }
    }
}


What I have tried:

At first I wrote
Java
class Quiz extends Student
{
    public static void main(String[] arg)
    {
        String Quiz = new Quiz();
        Quiz.name();
    }
}

This one is wrong but then change to this and still the error is there
Java
class Quiz extends Student
{
    public static void main(String[] arg)
    {
        Quiz obj = new Quiz();
        obj.name();
    }
}
Posted
Updated 28-Oct-21 8:55am
Comments
andrew samer 29-Oct-21 12:06pm    
There are three friend living on the straight line Ox in Lineland. The first friend lives at the point x1, the second friend lives at the point x2, and the third friend lives at the point x3. They plan to celebrate the New Year together, so they need to meet at one point. What is the minimum total distance they have to travel in order to meet at some point and celebrate the New Year?

Input: 3 number x1,x2,x3 as a house number.
Ex1: 7 1 4
Output: the minimum total distance they have to travel in order to meet.
Answer1: 6
It's guaranteed that the optimal answer is always integer.

obj is a variable whihc is declared in the Main method - so it is local to that method and does not exist outside it. This is called the scope of a variable, and it is limited to the pair of curly brackets within which an item is declared:
Java
// mt is not available here
if (...)
   {
   MyType mt = ...
   // mt is avaliable here
   ...
   }
// mt is not avaibale here
You don't want access to obj at all within the Quiz class as the code is running with the current instance set to this.

Think of it like a car: you put your mobile in the glove box of your car, then we go for a drive in my car. Do you expect to find your mobile in my car's glovebox? Or any "generic car" that happens to be around?
Car is the Class, GloveBox is the variable - this indicates which instance of a car you are taking about: if we are in your car, then "this.Glovebox" contains your mobile. If we are in my car, then "this.GloveBox" contains a box of nitrile gloves, some spare face masks, and an umbrella - but not your mobile!
 
Share this answer
 
This code is SERIOUSLY flawed.

Why do you keep extending the Question classes from the previously numbered Question class?

Why do have multiple main declarations in different Question classes?

Why have you extended the Quiz class from the Student class?

I suggest you get a good book on Java and read it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900