0

Pretty much what I want to do is to enter names and store them the same for surname and marks and then get the longest string in arraylist of names and then calculate average mark of 3 students from arraylist but I am currently stuck with storing things in array list

This is my class student:

class Student  {
    String name_student;
    String surname_student;
    float mark_student;

    public Student(String name_student, String surname_student, float mark_student) {
        this.name_student = name_student;
        this.surname_student = surname_student;
        this.mark_student = mark_student;
    }
}

This is mine main:

  public static void main(String[] args) {
        int i;
        String name = "";
        String surname = "";
        float mark = 0;
    ArrayList<Student> list = new ArrayList<>();
    for (i=0;i<3;i++){
         Scanner upisstudenata = new Scanner(System.in);
         Student student = new Student(name,surname,mark); 

         System.out.println("Enter name of student" );
         name = upisstudenata.nextLine();
         System.out.println("Enter surname of student" );
         surname = upisstudenata.nextLine();
         System.out.println("Enter mark of student" );
         mark = upisstudenata.nextFloat();
         list.add(new Student(name,surname,mark));
        } 
   }
  • What behavior are you seeing that you don't expect? Give us a little more information on how you are stuck – Tyler Mar 30 '20 at 13:56
  • See the duplicate Q/A. Also, don't keep creating a new Scanner object within the loop. Create only *one* Scanner based on System.in, and use it throughout your program. – Hovercraft Full Of Eels Mar 30 '20 at 13:56
  • Currently I am stuck I don't know how to take out separetly names of students from the arraylist to know if they are saved – Marin Spudić Mar 30 '20 at 13:58
  • @ Hovercraft Full Of Eels I fixed mine nextLine to Next() only now they work properly and moved scanner out of for loop forgot i left it there – Marin Spudić Mar 30 '20 at 14:03
  • You have fixed it incorrectly. The solution is not to get change any nextLine's to next(), but to add an additional `.nextLine()` after `.nextFloat()` to handle the end-of-line token. – Hovercraft Full Of Eels Mar 30 '20 at 14:18

0 Answers0