-2

The problem is that I need to print objects' info including arrays which contain students' marks. But it throws rubbish instead. Also arrays must have 5 elements

public class Student {
    Student(String fullname, int group, int marks[]){
        this.fullname = fullname;
        this.group = group;
        this.marks = marks;
    }//constructor
    void display(){
        System.out.println("Named: " + fullname + " | Group: " + group + " | Marks: " + marks);//prints out the objects
    }
    int group;
    int marks[];
    String fullname;//class fields
    public static void main(String[] args){
        Student Matthew = new Student("Whatever 1",14, new int[]{9, 6, 8, 4, 5});
        Student John = new Student("Whatever 2",13, new int[]{4, 9, 5, 10, 7});
        Student Max = new Student("Whatever 3",14, new int[]{7, 10, 8, 9, 9});//objects
        Matthew.display();
        John.display();
        Max.display();
    }
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Matthew
  • 19
  • 4

2 Answers2

0

Use Arrays.toString(marks) to print the array values

public class Student {
    Student(String fullname, int group, int marks[]){
        this.fullname = fullname;
        this.group = group;
        this.marks = marks;
    }//constructor
    void display(){
        System.out.println("Named: " + fullname + " | Group: " + group + " | Marks: " + Arrays.toString(marks));
    }
    int group;
    int marks[];
    String fullname;//class fields
    public static void main(String[] args){
        Student Matthew = new Student("Whatever 1",14, new int[]{9, 6, 8, 4, 5});
        Student John = new Student("Whatever 2",13, new int[]{4, 9, 5, 10, 7});
        Student Max = new Student("Whatever 3",14, new int[]{7, 10, 8, 9, 9});//objects
        Matthew.display();
        John.display();
        Max.display();
    }
}
Gaurav Dhiman
  • 953
  • 6
  • 11
0

If your'e not want to use the Arrays.toString() method (For aesthetic reasons) you can use that method:

public static String arrayToString(int[] array) {
        String arrayString = "";
        for (int i = 0; i < array.length; i++) {
            arrayString += array[i] + ", "; // Write here whatever you want to separate the array elements
        }
        return arrayString;
    }
Programmer
  • 803
  • 1
  • 6
  • 13