Code line ignored [duplicate]

6

The line marked with /*ISSO AQUI*/ is being ignored, ie the name for the second employee is not read, only the last name.

import java.util.Scanner;

public class EmployeeTest {

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Employee Empregado1 = new Employee();
    Employee Empregado2 = new Employee();

    System.out.print("Enter your name: ");
    String nome = input.nextLine();
    Empregado1.setName(nome);

    System.out.print("Enter your last name: ");
    String sobreNome = input.nextLine();
    Empregado1.setLastName(sobreNome);

    System.out.print("Enter you salary: ");
    double salario = input.nextDouble();
    Empregado1.setSalary(salario);

    double novoSalario = Empregado1.getSalary() + (0.10*Empregado1.getSalary());

    Empregado1.setSalary(novoSalario);
    Empregado1.showEmployee();

    //-----------------------------------------------------------------------------

    System.out.print("\n\nEnter your name: ");
    /*ISSO AQUI*/ nome = input.nextLine(); /*ISSO AQUI*/
    Empregado2.setName(nome);

    System.out.print("Enter your last name: ");
    sobreNome = input.nextLine();
    Empregado2.setLastName(sobreNome);

    System.out.print("Enter you salary: ");
    salario = input.nextDouble();
    Empregado2.setSalary(salario);

    novoSalario = Empregado2.getSalary() + (0.10*Empregado2.getSalary());

    Empregado2.setSalary(novoSalario);
    Empregado2.showEmployee();

    input.close();
}

}
    
asked by João Oliveira 27.02.2017 в 18:23
source

3 answers

12

This occurs because of a particularity of the % with% / a>. When you make this call:

System.out.print("Enter you salary: ");
double salario = input.nextDouble();

When you give enter , there is a line break that is not caught by Scanner . However, doing this:

System.out.print("\n\nEnter your name: ");
nome = input.nextLine(); /*ISSO AQUI*/

nextDouble() will get the string until the line break occurs. And since there is already an unrecorded line break, this method ends up consuming this line break and passing the code on.

One solution would be to catch the line break before you get there by adding a simple nextLine() shortly after input.nextLine() .

System.out.print("Enter you salary: ");
double salario = input.nextDouble();
input.nextLine();
Empregado1.setSalary(salario);

Remembering that this problem will occur every time you capture primitive data with string capture through nextDouble() , not only as nextLine() .

In this answer is a better solution to deal with this problem, although it seems a little more if implemented.

    
answered by 27.02.2017 / 18:33
source
4

Good afternoon,

The causer is in this line:

 double salario = input.nextDouble();

The function reads only to the next token and not the entire line, so when you call nome = input.nextLine(); it finishes reading the line.

You can use another input.nextLine(); to read the previous line, without assigning anything, or you can try:

//tente substituir
double salario = input.nextDouble();
// por
double salario = Double.parseDouble(input.nextLine());
    
answered by 27.02.2017 в 18:56
3

This is because nextDouble does not consume the last newline character of its entry, so the new line is consumed on the next call, one solution is to use next() instead of nextLine() , like this:

public static void main(String[] args) {

    Scanner input = new Scanner(System.in);
    Employee Empregado1 = new Employee();
    Employee Empregado2 = new Employee();

    System.out.print("Enter your name: ");
    String nome = input.next();
    Empregado1.setName(nome);

    System.out.print("Enter your last name: ");
    String sobreNome = input.next();
    Empregado1.setLastName(sobreNome);

    System.out.print("Enter you salary: ");
    double salario = input.nextDouble();
    Empregado1.setSalary(salario);

    double novoSalario = Empregado1.getSalary() + (0.10 * Empregado1.getSalary());

    Empregado1.setSalary(novoSalario);
    Empregado1.showEmployee();

    // -----------------------------------------------------------------------------

    System.out.print("\n\nEnter your name: ");
    /* ISSO AQUI */ nome = input.next(); /* ISSO AQUI */
    Empregado2.setName(nome);

    System.out.print("Enter your last name: ");
    sobreNome = input.next();
    Empregado2.setLastName(sobreNome);

    System.out.print("Enter you salary: ");
    salario = input.nextDouble();
    Empregado2.setSalary(salario);

    novoSalario = Empregado2.getSalary() + (0.10 * Empregado2.getSalary());

    Empregado2.setSalary(novoSalario);
    Empregado2.showEmployee();

    input.close();
} 
    
answered by 27.02.2017 в 18:40