Olá, gostaria de uma ajuda se possivel, estou resolvendo um exercício do livro Java How to Program 9th Edition - Deitel, e me deparei com o seguinte problema: O método nextLine que foi varias vezes utilizado é ignorado durante a execução do programa após a execução de um nextDouble. Gostaria de saber porque isso está acontecendo, a propósito o compilador não está retornando nem um erro.
A ideia é a seguinte: eu crio uma classe chamada Employee e depois uma outra chamada EmployeeTest para testar suas funcionalidades.
A linha que está sendo ignorada é a 34 do arquivo EmployeeTest.java.
Employee.java
// Ex. 3.14: Employee.java
public class Employee
{
// instance variables
private String name;
private String lastName;
private double monthlySalary;
// constructor
public Employee( String nam, String las, double mon )
{
name = nam;
lastName = las;
if ( mon >= 0.0 )
monthlySalary = mon;
}
// method to set the name
public void setName( String nam )
{
name = nam;
} // end method setName
// method to retrieve the name
public String getName()
{
return name;
} // end method getName
// method to set the lastName
public void setLastName( String las )
{
lastName = las;
} // end method setLastName
// method to retrieve the lastName
public String getLastName()
{
return lastName;
} // end method getLastName
// method to set the monthlySalary
public void setMonthlySalary( double mon )
{
if ( mon >= 0.0 )
monthlySalary = mon;
} // end method setMonthlySalary
// method to retrieve the monthlySalary
public double getMonthlySalary()
{
return monthlySalary;
} // end method getMonthlySalary
} // end class Employee
EmployeeTest.java
// Ex. 3.14: EmployeeTest.java
import java.util.Scanner;
public class EmployeeTest
{
public static void main( String[] args )
{
Employee employee1 = new Employee( "João", "Silva", 1200.00 );
Employee employee2 = new Employee( "Maria", "Rocha", 1500.00 );
Scanner input = new Scanner( System.in );
// employee1
System.out.printf( "employee1\n\nName: %s\nLast Name: %s\nMonthly Salary: %.2f\n",
employee1.getName(), employee1.getLastName(), employee1.getMonthlySalary() );
System.out.println( "\nModify employee1" );
System.out.print( "Enter the new name for employee1: " );
employee1.setName( input.nextLine() );
System.out.print( "Enter the new last name for employee1: " );
employee1.setLastName( input.nextLine() );
System.out.print( "Enter the new monthly salary for employee1: " );
employee1.setMonthlySalary( input.nextDouble() );
System.out.println( "\nChanges applied.\n\n" );
System.out.printf( "employee1\n\nName: %s\nLast Name: %s\nMonthly Salary: %.2f\n",
employee1.getName(), employee1.getLastName(), employee1.getMonthlySalary() );
// employee2
System.out.printf( "\n\nemployee2\n\nName: %s\nLast Name: %s\nMonthly Salary: %.2f\n",
employee2.getName(), employee2.getLastName(), employee2.getMonthlySalary() );
System.out.println( "\nModify employee1" );
System.out.print( "Enter the new name for employee2: " );
employee2.setName( input.nextLine() ); // >>>> ESTA É A LINHA IGNORADA <<<<
System.out.print( "Enter the new last name for employee2: " );
employee2.setLastName( input.nextLine() );
System.out.print( "Enter the new monthly salary for employee2: " );
employee2.setMonthlySalary( input.nextDouble() );
System.out.println( "\nChanges applied.\n\n" );
System.out.printf( "employee2\n\nName: %s\nLast Name: %s\nMonthly Salary: %.2f\n",
employee2.getName(), employee2.getLastName(), employee2.getMonthlySalary() );
} // end main
} // end class EmployeeTest

