扫描仪没有阻止输入

时间:2013-09-11 21:02:52

标签: java oop

我能够解决我的最后一个问题,然而,其中一个人建议我为此提出一个新问题,所以我们在这里。下面这段代码没有运行,是EmployeeTest.java主类的一部分。

//change the company name
    String newCompanyName;
    System.out.println("Please enter a new company name:");
    newCompanyName = conIn.nextLine();
    Employees.setCompanyName(newCompanyName);

上述代码的输出是:

---------------------------------------

Please enter a new company name:

---------------------------------------

它只打印该代码和输出之前和之后的所有内容。它从不要求输入。

以下代码是EmployeeTest.java的全部内容

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package employeerionmurphy;
import employeerionmurphy.Employees.*;
import java.util.*;

/**
 *
 * @author Owner
 */
public class EmployeeTest {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here

        //declare scanner
        Scanner conIn = new Scanner(System.in);

        //instantiate 4 employee objects
        Employees worker1 = new Employees();
        Employees worker2 = new Employees();
        Employees worker3 = new Employees();
        Employees worker4 = new Employees();

        //Display the Employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //declare variables to update Employees
        String newFirstName1, newFirstName2, newFirstName3, newFirstName4;
        String newLastName1, newLastName2, newLastName3, newLastName4;
        String newAddress1, newAddress2, newAddress3, newAddress4;
        String newCity1, newCity2, newCity3, newCity4;
        int newHoursWorked1, newHoursWorked2, newHoursWorked3, newHoursWorked4;
        int newWageRate1, newWageRate2, newWageRate3, newWageRate4;

        //update the employee (in progress)
        //upating worker1
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);
        worker1.setAddress(newAddress1);
        worker1.setCity(newCity1);
        worker1.setHoursWorked(newHoursWorked1);
        worker1.setWageRate(newWageRate1);

        //upating worker1
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);
        worker1.setAddress(newAddress1);
        worker1.setCity(newCity1);
        worker1.setHoursWorked(newHoursWorked1);
        worker1.setWageRate(newWageRate1);

        //upating worker1
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);
        worker1.setAddress(newAddress1);
        worker1.setCity(newCity1);
        worker1.setHoursWorked(newHoursWorked1);
        worker1.setWageRate(newWageRate1);

        //upating worker1
        System.out.println("Please enter Employee 1's first name:");
        newFirstName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's last name:");
        newLastName1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's address:");
        newAddress1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's city:");
        newCity1 = conIn.nextLine();
        System.out.println("Please enter Employee 1's hours worked:");
        newHoursWorked1 = conIn.nextInt();
        System.out.println("Please enter Employee 1's wage rate:");
        newWageRate1 = conIn.nextInt();
        worker1.setFirstName(newFirstName1);
        worker1.setLastName(newLastName1);
        worker1.setAddress(newAddress1);
        worker1.setCity(newCity1);
        worker1.setHoursWorked(newHoursWorked1);
        worker1.setWageRate(newWageRate1);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //upgrade the wage rate of the employee that has a wage rate of zero
        //(in progress)


        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display Employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the Employees name and gross pay
        System.out.println(worker1.getTotalPay());
        System.out.println(worker2.getTotalPay());
        System.out.println(worker3.getTotalPay());
        System.out.println(worker4.getTotalPay());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //change the company name
        String newCompanyName;
        System.out.println("Please enter a new company name:");
        newCompanyName = conIn.nextLine();
        Employees.setCompanyName(newCompanyName);

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("");

        //display the Employees
        System.out.println("Employee 1");
        System.out.println("----------------------");
        System.out.println(worker1.getInfo());
        System.out.println("Employee 2");
        System.out.println("----------------------");
        System.out.println(worker2.getInfo());
        System.out.println("Employee 3");
        System.out.println("----------------------");
        System.out.println(worker3.getInfo());
        System.out.println("Employee 4");
        System.out.println("----------------------");
        System.out.println(worker4.getInfo());

        //spacer for user readability
        System.out.println("");
        System.out.println("--------------------------------------------");
        System.out.println("Built by Rion Murphy");

    }
}

我对此感到非常困惑,没有错误,也没有任何错误。只是不跑。我已经查看了代码,但我认为没有任何理由说它不起作用。

1 个答案:

答案 0 :(得分:0)

有问题的代码之前的代码是

System.out.println("Please enter Employee 1's hours worked:");
newHoursWorked1 = conIn.nextInt();
System.out.println("Please enter Employee 1's wage rate:");
newWageRate1 = conIn.nextInt();

从标准输入中读取int个值。 nextInt()调用仅读取int值,但保留\r\n换行符。因此,您的后续nextLine()将消耗该值,而不是等待输入。

添加

conIn.nextLine();

在您的有问题的代码之前使用悬挂的行尾字符。所以

conIn.nextLine();

String newCompanyName;
System.out.println("Please enter a new company name:");
newCompanyName = conIn.nextLine();
Employees.setCompanyName(newCompanyName);