0

I was able to solve my last problem, however, one of the guys suggested I make a new question for this, so here we are. This piece of code below is not running and is part of the EmployeeTest.java main class.

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

The output for the above code is:

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

Please enter a new company name:

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

It just prints everything before and after that code and output. It never asks for an input.

The following code is the entirety of 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");

    }
}

I am quite baffled by this, there is no error, no nothing. Just doesn't run. I've reviewed the code, and I don't see any reason why it shouldn't work.

4

1 回答 1

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);
于 2013-09-11T21:15:51.707 回答