Scanner.nextLine does not pick up information after Scanner.nextInt [duplicate]

this question already has answers here : How to use Java scanner (2 responses) Closed 4 years ago.

I'm having problem in this code, I'm starting in programming, this is just an activity for practice. I do not understand why when executing the code the field 'Campus' does not allow the input of the information. Now move to the 'enrollment'field

Campus: Tuition (Numbers only):

I remember, it's just an early practice of the programming discipline, nothing too far-fetched.

package classescanner;

import java.util.Scanner;
/**
 * @author magnolia
 */
public class ClasseScanner {

    public static void main(String[] args) {

        String nome, sexo, matricula;
        String campus;
        int ano;
        double nota1, nota2, media;


        Scanner entrada = new Scanner (System.in);
        String escola = "IFRN CAMPUS ";

        System.out.println("------------------------------");
        System.out.println("DIGITE AS INFORMAÇÕES DO ALUNO");
        System.out.println("------------------------------");

        System.out.print("Nome completo: ");
        nome = entrada.nextLine();

        System.out.print("Sexo: ");
        sexo = entrada.nextLine();

        System.out.print("Ano de nascimento (yyyy): ");
        ano = entrada.nextInt();

        System.out.print("Campus: ");
        campus = entrada.nextLine();

        System.out.print("Matrícula (Apenas números): ");
        matricula = entrada.nextLine();

        System.out.print("Nota 1: ");
        nota1 = entrada.nextDouble();

        System.out.print("Nota 2: ");
        nota2 = entrada.nextDouble();

        media = (nota1 + nota2) / 2;

        System.out.println("--------------");
        System.out.println("SITUAÇÃO FINAL");
        System.out.println("--------------");

        System.out.println(escola.concat(campus).toUpperCase());
        System.out.println("Matrícula nº: " + matricula);
        System.out.println("Aluno(a): " + nome.toUpperCase() + " - Sexo: " + sexo.substring(0,1).toUpperCase() + " - Nascido(a) em: " + ano);

        if (media >= 60){

            System.out.println("Status: APROVADO(A)");
        }
        else {
            System.out.println("Status: REPROVADO(A)");
        }        
    }    
}
Author: MagShania, 2016-11-12

1 answers

According to the documentation of the Scanner class the method nextLine returns the rest of the last line that was read, which in the case is only an enter, so the reading is ignored. So following the guidance of this topic How to use the Java scanner I modified the code to use only nextLine and perform the conversion, doing the type check.

import java.util.Scanner;

public class ClasseScanner {

  private static Scanner entrada;

  public static void main(String[] args) {
    String nome, sexo, matricula;
    String campus;
    int ano;
    double nota1, nota2, media;

    entrada = new Scanner(System.in);
    String escola = "IFRN CAMPUS ";

    try {
      System.out.println("------------------------------");
      System.out.println("DIGITE AS INFORMAÇÕES DO ALUNO");
      System.out.println("------------------------------");

      System.out.print("Nome completo: ");
      nome = entrada.nextLine();

      System.out.print("Sexo: ");
      sexo = entrada.nextLine();

      System.out.print("Ano de nascimento (yyyy): ");
      ano = lerInteiro();

      System.out.print("Campus: ");
      campus = entrada.nextLine();

      System.out.print("Matrícula (Apenas números): ");
      matricula = entrada.nextLine();

      System.out.print("Nota 1: ");
      nota1 = lerNumerico();

      System.out.print("Nota 2: ");
      nota2 = lerNumerico();

      media = (nota1 + nota2) / 2;

      System.out.println("--------------");
      System.out.println("SITUAÇÃO FINAL");
      System.out.println("--------------");

      System.out.println(escola.concat(campus).toUpperCase());
      System.out.println("Matrícula nº: " + matricula);
      System.out.println("Aluno(a): " + nome.toUpperCase() + " - Sexo: " + sexo.substring(0, 1).toUpperCase() + " - Nascido(a) em: " + ano);

      if (media >= 60) {

        System.out.println("Status: APROVADO(A)");
      } else {
        System.out.println("Status: REPROVADO(A)");
      }
    } catch (NumberFormatException ex) {
      System.out.println("O valor digitado não é um número válido!");
    }
  }

  private static int lerInteiro() {
    String digitado = "";

    digitado = entrada.nextLine();

    return Integer.parseInt(digitado);
  }

  private static double lerNumerico() {
    String digitado = "";

    digitado = entrada.nextLine();

    return Double.parseDouble(digitado);
  }
}

Resulting in:

run:
------------------------------
DIGITE AS INFORMAÇÕES DO ALUNO
------------------------------
Nome completo: Lucas S
Sexo: Masculino
Ano de nascimento (yyyy): 1988
Campus: Ponta Grossa
Matrícula (Apenas números): 84025350
Nota 1: 100
Nota 2: 80
--------------
SITUAÇÃO FINAL
--------------
IFRN CAMPUS PONTA GROSSA
Matrícula nº: 84025350
Aluno(a): LUCAS S - Sexo: M - Nascido(a) em: 1988
Status: APROVADO(A)
CONSTRUÍDO COM SUCESSO (tempo total: 19 segundos)
 2
Author: Sorack, 2017-04-13 12:59:43