在Java中打印连接字符串时出现问题
系统会提示用户逐行输入员工数据的值。我选择扫描整行数据,然后将每段数据分隔成一个字符串数组(用空格分隔)。我创建了变量fullName并连接了员工的名字和姓氏,但当我打印代码时,它只显示姓氏。我已经对此进行了大约三个小时的故障排除,没有发现任何语法或逻辑错误,为什么不打印全名
\
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
/**
* Employee Record Class
*
* @Theodore Mazer
* @version 9/8/15
*/
public class EmployeeRecord
{
ArrayList<String> names = new ArrayList<String>();
ArrayList<String> taxIDs = new ArrayList<String>();
ArrayList<Double> wages = new ArrayList<Double>();
private String employeeId = "%03d";
private String taxID;
private double hourlyWage = 0.0;
public ArrayList<String> getNamesArrayList(){ //getter method for employee names
return names;
}
public ArrayList<String> getTaxIdsArrayList(){ //getter method for tax IDs
return taxIDs;
}
public ArrayList<Double> getWagesArrayList(){ //getter method for hourly wages
return wages;
}
public void setEmployeeData(){ //setter method for employee data entry
Scanner scan = new Scanner(System.in);
String firstName = "";
String lastName = "";
String info = "";
System.out.println("Enter each employees full name, tax ID, and hourly wage pressing enter each time. (Enter the $ key to finish)");
while(!(scan.next().equals("$"))){
info = scan.nextLine();
String[] splitString = info.split(" ");
String fullName = "";
firstName = splitString[0];
lastName = splitString[1];
fullName = firstName + " " + lastName;
double hWage = Double.parseDouble(splitString[3]);
names.add(fullName);
taxIDs.add(splitString[2]);
wages.add(hWage);
}
System.out.println("Employee ID | Employee Full Name | Tax ID | Wage ");
for(int i = 0; i <= names.size() - 1; i++){
System.out.printf(String.format(employeeId, i + 1) + " | " + names.get(i) + " | " + taxIDs.get(i) + " | " + wages.get(i));
System.out.println();
}
}
}
# 1 楼答案
在
while
条件下,您使用的next()
消耗下一个令牌,在您的情况下,它是第一个名称我将对while循环进行两个修改: