Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 扫描仪未阻止输入_Java_Oop - Fatal编程技术网

Java 扫描仪未阻止输入

Java 扫描仪未阻止输入,java,oop,Java,Oop,我能够解决我的上一个问题,但是,其中一个家伙建议我为此提出一个新问题,所以我们来了。下面的这段代码没有运行,是EmployeeTest.java主类的一部分 //change the company name String newCompanyName; System.out.println("Please enter a new company name:"); newCompanyName = conIn.nextLine(); Employees.setCo

我能够解决我的上一个问题,但是,其中一个家伙建议我为此提出一个新问题,所以我们来了。下面的这段代码没有运行,是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");

    }
}

这让我很困惑,没有错误,什么都没有。就是跑不动。我已经检查了代码,没有任何理由认为它不应该工作。

在问题代码出现之前检查代码

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

调用后,
newCompanyName
的内容是什么?如果查看
worker[1-3]的输出,就会得到提示。getInfo()
@chrylis将是
\n
。“我想你以前已经回答过这样一个问题了。”里昂默夫墨菲。尝试使用
conIn.next()
而不是
conIn.nextLine()
此外,请使用数组,而不仅仅是在变量名中添加数字。