Pranav Ghate
Pranav Ghate

Reputation: 57

Error:Cannot find symbol.. in My Java Program

This is my whole code Getting the following error in command prompt.Please Help me to solve it, a beginner here.

"Student.java:36: error: cannot find symbol
                        s[i].Student();
                            ^
  symbol:   method Student()
  location: class Student
1 error"

    import java.util.*;

public class Student
{
    int roll;
    String name=new String();


    Student()
    {
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Name");
        name=sc.next();
        System.out.println("Enter Roll No");
        roll=sc.nextInt();
    }

    public String toString()
    {
        return "Name:"+name+" "+"Roll Number:"+roll ;
    }
}

class Main
{
    public static void main(String args[])
    {
        int n,i;
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter the number of students");
        n=sc.nextInt();
        Student s[]=new Student[n];
        for(i=0;i<n;i++)
        {
            s[i]=new Student();
            s[i].Student();
        }
        for(i=0;i<n;i++)
        {
            System.out.println(s[i]);
        }
    }
}

Upvotes: 0

Views: 1037

Answers (4)

Kim Honoridez
Kim Honoridez

Reputation: 997

I think you're calling the constructor like s[i].Student() which is incorrect.

Java will assume that Student() is a method and not a constructor. Since you don't have a method normal Student() method in your class, for sure, it will not be found.

Remember, constructor is not just a plain method. It's a special method to instantiate objects.

Upvotes: 0

pedromss
pedromss

Reputation: 2453

Also you don't need a class Main, take your method main and put it inside your class Student, remove this line:

s[i].Student();

And the program runs fine

Upvotes: 0

scadge
scadge

Reputation: 9733

You just don't need that line: s[i].Student();

The Student() constructor is called when you write new Student().

Read this about constructors in Java.

Upvotes: 0

maksimov
maksimov

Reputation: 5811

On line 35 you are correctly instantiating a class with the constructor,

s[i]=new Student();

On line 36 you're incorrectly (and for no apparent logical reason?) calling a constructor like a method. Remove this line:

s[i].Student();

Upvotes: 1

Related Questions