Ignored line of code

Asked

Viewed 307 times

5

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();
}

}

3 answers

10


This occurs because of a particularity of class methods Scanner. When you make this call:

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

By giving the enter, there is a line break that is not captured by nextDouble(). But in doing so:

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

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

One solution would be to capture the line break before getting there by adding a simple input.nextLine() shortly after the nextDouble().

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

Remembering that this problem will occur whenever you perform primitive data capture with string capture through the nextLine(), and not only as the nextDouble().

In this answer is given a better solution to deal with this problem, although it seems a little more laborious to implement.

3

Good afternoon,

The culprit is on this line:

 double salario = input.nextDouble();

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

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

//tente substituir
double salario = input.nextDouble();
// por
double salario = Double.parseDouble(input.nextLine());
  • In fact what nextdouble does not read is the line break left by enter, and as nextLine consumes the string until there is a line break, it ends up consuming the line break left by nextdouble.

  • Thanks for the correction.

2

This is because nextDouble does not consume the last new line character of your input and therefore the new line is consumed in the next call, one solution is to use the next() instead of nextLine(), in this way:

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();
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.