0

So, I recently started class and objects in java and was solving the question: Create a class named 'Student' with String variable 'name' and integer variable 'roll_no'. Assign the value of roll_no as '2' and that of name as "John" by creating an object of the class Student.

This is the code I wrote:

public class Student {
    
    String name;
    int roll_no;
}

I saved this in one file and

import java.util.Scanner;

public class Studentdemo {
    
    public static void main(String[] args) {

        Scanner sc = new  Scanner(System.in);
        
        Student s = new Student();

        System.out.print("Enter the name: ");
        s.name = sc.next();

        System.out.print("Enter the roll number: ");
        s.roll_no = sc.nextInt();

        System.out.println("Name = " + s.name + "\nRoll No. = " + s.roll_no);
    }
}

I saved this in another file and tried to compile and run this code

error: cannot find symbol
        Student s = new Student();
        ^
  symbol:   class Student
  location: class Studendemo
ef.java:9: error: cannot find symbol
        Student s = new Student();
                        ^
  symbol:   class Student
  location: class Studendemo
2 errors
error: compilation failed

I tried to run this code in an online compiler, it was running fine on that but not on ubunt

Kevin Hooke
  • 2,583
  • 2
  • 19
  • 33
  • Are the classes in the same directory, did you compile them both? (first Student, then StudentDemo)? This is not an Ubuntu issue, it's likely you forgot to compile Student first – Stultuske Mar 31 '21 at 06:14
  • _tried to compile ... this code_ How did you try to compile? Did you enter the `javac` command in a terminal window? If yes, then [edit] your question and post the entire command, exactly as you entered it. – Abra Mar 31 '21 at 06:18
  • yes the classes are in the same directory, I compiled the first (Student) but it couldn't as there is no void main method in it, I even tried to use the 'extends' function as I made two new files with the same names as their classes and then I compiled StudentDemo as it included the main method and as it also had the class Student but the error was not fixed as it still showed that " could not find symbol " – DunkinSomeDonuts Mar 31 '21 at 11:42

0 Answers0