Scanner does not read the last parameter



  • The purpose of my program is to complete data on the Andrei site from the co-author. The Scanner's function takes all the meanings of the last cross. I can't fill the Color value. Instead, the field information is immediately on the screen. I'm putting the result code in the console below.

    ua.com.foxminded.school.School
    Enter name of the first student: 
    Andrei Kulagin
    Enter gender of the first student: 
    Male
    Enter age of the first student: 
    24
    Enter score of the first student: 
    5
    Enter color of the first student: 
    A{name='Andrei Kulagin', gender='Male', age=24, score=5, color=''}
    

    Process finished with exit code 0

    I'm following my code:

    package ua.com.foxminded.school;
    import java.util.Scanner;

    public class School {
    public static void main (String[] args){

        A Andrei = new A();
        A Dima = new A();
        A Alex = new A();
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter name of the first student: ");
        Andrei.name = scanner.nextLine();
        System.out.println("Enter gender of the first student: ");
        Andrei.gender = scanner.nextLine();
        System.out.println("Enter age of the first student: ");
        Andrei.age = scanner.nextInt();
        System.out.println("Enter score of the first student: ");
        Andrei.score = scanner.nextInt();
        System.out.println("Enter color of the first student: ");
        Andrei.color = scanner.nextLine();
        System.out.println(Andrei);
    
    
    }
    

    }

    Class A code:

    package ua.com.foxminded.school;

    public class A {
    String name;
    String gender;
    int age;
    int score;
    String color;

    @Override
    public String toString() {
        return "A{" +
                "name='" + name + '\'' +
                ", gender='" + gender + '\'' +
                ", age=" + age +
                ", score=" + score +
                ", color='" + color + '\'' +
                '}';
    }
    

    }



  • scanner.nextInt() waiting list
    scanner.nextLine() waiting and reading the end of the line"\n")

    Once the number has been inserted and the enter will automatically work. nextLine()
    Replace scanner.nextLine()scanner.next()



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2