0

Everthing that I wanted to work has worked and now the final part, I need to check if a student of certain id has certain vaccine or not and the result should be in true or false only. I know that I have to use +listofInfos.contains():' but I don't know how to use it nor where to use it.

import java.util.ArrayList;
import java.util.Scanner;

public class Student {

    private static class Info {
        private String student_name;
        private int student_id;
        private String vaccine_name;
        private int vaccine_id;
        
        public Info(String student_name, String vaccine_name, int student_id, int vaccine_id) {
            this.student_name = student_name;
            this.student_id = student_id;
            this.vaccine_name = vaccine_name;
            this.vaccine_id = vaccine_id;
        }
        
        public String getStudentName() {
            return student_name;
        }
        
        public int getStudentId() {
            return student_id;
        }
        
        public String getVaccineName() {
            return vaccine_name;
        }
        
        public int getVaccineId() {
            return vaccine_id;
        }
    }
    
    public static void main(String[] args) {
        ArrayList<Info> listofInfos = new ArrayList<Info>();
        addInfo(listofInfos);
    }

    private static void addInfo(ArrayList<Info> listofInfos) {
        Scanner myKB = new Scanner(System.in);
        System.out.println("How many datas do you want to input?");
        int b = myKB.nextInt();

        for (int i = 0; i <=(b-1); i++) {
            myKB.nextLine();
            System.out.println("Enter Student  Name:");
            String student_name = (myKB.nextLine());
            
            System.out.println("Enter Vaccine Name:");
            String vaccine_name = (myKB.nextLine());
            
            System.out.println("Enter StudentID:");
            int student_id = (myKB.nextInt());
            
            System.out.println("Enter Vaccine Id:");
            int vaccine_id = (myKB.nextInt());
            
            listofInfos.add(new Info(student_name,  vaccine_name, student_id, vaccine_id ));
            
        }
        
        for (Info List : listofInfos) {
            System.out.println("Student Name:"+ List.getStudentName() + "\tStudent ID:" + List.getStudentId() + "\tVaccine Name:" + List.getVaccineName() + "\tVaccine ID:" + List.getVaccineId());
        }
    }
}
tienph
  • 1,239
  • 13
  • 19
  • 1
    Every time you call `nextInt()` you will be left with a line feed in the input buffer. So if you then prompt again for input with `nextLine()`, the `Scanner` will read *that* empty input with its newline and *not* the one you prompted for – g00se May 07 '22 at 13:51
  • Check this: https://stackoverflow.com/a/13102066/6178740 – suspicioususer May 07 '22 at 13:59

1 Answers1

0

You should change myKB.nextline() to myKB.next(). nextInt() will read a number, but it will leave blanks, that is to say, the focus will not move to the next line, it is still in this line.

nextLine() reads the rest of the line, including newlines, and moves the focus to the beginning of the next line.

next() receives the next String type variable with a newline or whitespace as the boundary.

So you just use next() instead of nextLine() to read from a new line.