0

I need to create an object array and read the values of the elements in the constructor from console. I am totally confused how to do it.Can anyone give a clarity about how to do it

public class Student {
    int id;
    String name;
    double marks;

    public Student(int id, String name, double marks) {
        id = this.id;
        name = this.name;
        marks = this.marks;
    }
}

public class Solution {
    public static void main(String[],args)
    {
      Scanner sc = new Scanner(System.in);
      int n=sc.nextInt();
      Student[] arr=new Student[n];
      for(int i=0;i<n;i++)
      {
         int x =sc.nextInt();
         String y=sc.nextLine();
         double z=sc.nextDouble();
         arr[i]=arr.Student(x,y,z);
      }
    }
}

I am confused on how to call the constructor. Can anyone help me?

2
  • 4
    arr[i]=new Student(x,y,z); Commented Feb 17, 2019 at 8:56
  • 3
    Additionally, you're setting variables incorrectly in your constructor: id = this.id; should be this.id = id; (same for the other 2 variables) Commented Feb 17, 2019 at 8:59

4 Answers 4

1

You can do one of two things:

1.Create a temporary object by calling the constructor and then adding that object in the array:

Student temp= new Student(x,y,z);
arr[i]=temp;

2.Directly instantiate a new object and add it in the array like this:

arr[i]=new Student(x,y,z);

Both methods will work fine, but it is recommended to use method 2 because you should not allocate memory to a temp object when clearly you can achieve the goals without doing so

Sign up to request clarification or add additional context in comments.

Comments

1

Instead of:

arr[i]=arr.Student(x,y,z);

Do:

arr[i]=new Student(x,y,z);

Why? Because, Each object in the array is an instance of Student class

Comments

1

Your constructor is declared wrongly. this is always used to refer to the instance variable. Change your constructor to this:

public class Student {
int id;
String name;
double marks;

public Student(int id, String name, double marks) {
    this.id = id;
    this.name = name;
    this.marks = marks;
} }

public class Solution {
    public static void main(String[],args)
    {
      Scanner sc = new Scanner(System.in);
      int n=sc.nextInt();
      Student[] arr=new Student[n];
      for(int i=0;i<n;i++)
      {
         int x =sc.nextInt();
         String y=sc.nextLine();
         double z=sc.nextDouble();
         arr[i]= new Student(x,y,z); //no need to create an object for arr
      }
    }
}

Comments

-1

Because your constructor is wrong.

public class Student {
    int id;
    String name;
    double marks;

    public Student(int id, String name, double marks) {
        this.id = id;
        this.name = name;
        this.marks = marks;
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.