0

So, this is the sample code that we are supposed to use as reference for a formative assessment. I decided to run it in the terminal to see if it would work, and it always result in "Error Cannot Find Symbol". Would appreciate if someone could tell me how to fix it.

Here's the error message:

/Testing.java:6: error: cannot find symbol
        Person p1 = new Person();
        ^
  symbol:   class Person
  location: class Testing
/Testing.java:6: error: cannot find symbol
        Person p1 = new Person();
                        ^
  symbol:   class Person
  location: class Testing
/Testing.java:7: error: cannot find symbol
        Person p3 = new Person("Chris", 30);
        ^
  symbol:   class Person
  location: class Testing
/Testing.java:7: error: cannot find symbol
        Person p3 = new Person("Chris", 30);
                        ^
  symbol:   class Person
  location: class Testing
/Testing.java:10: error: cannot find symbol
        Person p2;
        ^
  symbol:   class Person
  location: class Testing
/Testing.java:12: error: cannot find symbol
        p2 = new Person();
                 ^
  symbol:   class Person
  location: class Testing
6 errors

And here's the code:

public class Testing{
    public static void main(String [] args){
        //declare and instantiate;
        
        Person p1 = new Person();
        Person p3 = new Person("Chris", 30);

        //declare
        Person p2;
        //instantiate;
        p2 = new Person();

        //Assign values for p1 and p2
        /*
        p1.name="Lawrence";
        p2.name="Percy";

        p1.age=20;
        p2.age=-5;
        */
        p1.setName("Lawrence");
        p2.setName("Percy");
        


        p1.setAge(20);
        p2.setAge(-5);
        

        //Display values
        //System.out.println(p1.name + " at age " + p1.age);
        //System.out.println(p2.name + " at age " + p2.age);

        p1.showDetails();
        p2.showDetails();
        System.out.println(p3.getName() + " at age " + p3.getAge());



    }
}

Edit: People asked for the person class as well, so I'm putting it here:


public class Person{
    //variables
    private String name;
    private int age;


    //constructor
    public Person(){
        //nothing here...
    }

    public Person(String name, int age){
        setName(name);
        setAge(age);
    }

    //methods
    public void showDetails(){
        System.out.println(name + " at age " + age);
    }

    //Setters
    public void setName(String name){
        this.name=name;
    }

    public void setAge(int age){
        if(age>0)
            this.age=age;
        else
            this.age=0;

    }

    //Getters
    public String getName(){
        return name;
    }

    public int getAge(){
        return age;
    }

}

class A{

}

Confusion
  • 1
  • 1

2 Answers2

1

If you are trying to run your code via the command java Testing.java you will need to have previously compiled any other class, i.e.:

$ javac Person.java
$ java Testing.java

This assumes that Person is in the default package, i.e. there is no package statement in Person.java.

If either Person.java or Testing.java have a package ...; statement, remove it.

Using the code which is in your question, this works:

tgdavies@DESKTOP-A4LJAJE:~/tmp$ ls
Person.java  Testing.java
tgdavies@DESKTOP-A4LJAJE:~/tmp$ javac Person.java
tgdavies@DESKTOP-A4LJAJE:~/tmp$ ls
A.class  Person.class  Person.java  Testing.java
tgdavies@DESKTOP-A4LJAJE:~/tmp$ java Testing.java
Lawrence at age 20
Percy at age 0
Chris at age 30

Notice the .class files which are produced by javac.

tgdavies
  • 10,307
  • 4
  • 35
  • 40
  • Hello, I tried doing this, and while the terminal did accept javac Person.java, the same issues still persisted. I also added in a package statement for both files. – Confusion Mar 12 '22 at 08:51
  • If you have package statements the above won't work, I'll update the answer to make that clearer. – tgdavies Mar 12 '22 at 10:06
-2

Please import the package name of Person or, declare two persons class