0
import java.io.*;

import java.util.*;
class Student implements Comparable<Student>{
String name;
int rno;
Student(String name, int rno){
    this.name = name;
    this.rno = rno;
} // end of constructor
public boolean equals(Object o){
    Student st = (Student)o;
    return rno == st.rno;
}
public int compareTo(Student s){
    return rno-s.rno;
}
public String toString(){
    return name+" "+rno;
}
}   

class demo{
public static void main(String args[]){
    Console c = System.console();
    int marks [] = new int[4];
    TreeSet <Student> sinfo = new TreeSet<Student>();
    TreeMap<Student,int[]> full_info = new TreeMap<>();
    int no_student = Integer.parseInt(c.readLine("Enter the number of students: "));
        for(int j = 0;j<no_student;j++){
            String sname = c.readLine("Enter"+(j+1)+" Student name: ");
            int srno = Integer.parseInt(c.readLine("Enter "+sname+"'s Roll.No. : "));
            Student s = new Student(sname,srno);
            sinfo.add(s);
            int english = Integer.parseInt(c.readLine("Enter the marks of English: "));
            int maths = Integer.parseInt(c.readLine("Enter the marks of Maths: "));
            int hindi = Integer.parseInt(c.readLine("Enter the marks of Hindi: "));
            int sci = Integer.parseInt(c.readLine("Enter the marks of Science: "));
                for(int i = 0; i<marks.length; i++){
                if(i == 0){marks[i]= english;}
                    else if(i == 1){marks[i]= maths;}
                        else if(i == 2){marks[i]=hindi;}
                            else{marks[i] = sci;}
                }// end of for for marks entry
        full_info.put(s,marks); 
        }
for (Map.Entry<Student, int[]> 
             entry : full_info.entrySet()) 
        System.out.println( 
            "[" + entry.getKey() 
            + ", " + entry.getValue() + "]");

}// end of main
}// end of demo

This is the output am getting

  1. D:\java practice>java demo
  2. Enter the number of students: 1
  3. Enter1 Student name: swapnil
  4. Enter swapnil's Roll.No. : 101
  5. Enter the marks of English: 10
  6. Enter the marks of Maths: 20
  7. Enter the marks of Hindi: 30
  8. Enter the marks of Science: 40
  9. [swapnil 101, [I@340f438e]

What changes Should i make in order to get the output has [10,20,30,40] instead of the hashcode? Where Should i override the toString method to get the value?

Swapnil Padaya
  • 687
  • 5
  • 14

0 Answers0