我正在创建一个程序,该程序从外部文件读取数据,将其与文件中的其他数据进行比较,然后将结果打印到新的外部文件。我的代码的 while 循环部分遇到问题。我不确定是 while 循环本身还是嵌套在其中的 for 循环。这是代码:
public class WageCalculator {
public static void main(String[] args) throws FileNotFoundException {
Scanner input = new Scanner(new FileReader("TestData.txt")); //Scanner for external file
PrintWriter output = new PrintWriter("wagedaily.txt");
float RecommendedMaximum;
RecommendedMaximum = Float.parseFloat(JOptionPane.showInputDialog(null, "Enter the recommended maximum journey cost:"));
String ShipID, JourneyID; //Variables
int JourneyLength, Crew;
double RateOfPay, CrewCost, TotalCost;
while (input.hasNext()) { //EOF-Controlled While Loop
ShipID = input.nextLine();
JourneyID = input.nextLine();
JourneyLength = input.nextInt();
Crew = input.nextInt();
CrewCost = 0; //Default Values Set
TotalCost = 0;
for (int x = Crew; x > 0; x--) { //For Loop updates the above values
RateOfPay = input.nextDouble();
CrewCost = RateOfPay * JourneyLength;
TotalCost = TotalCost + CrewCost;
}
if (TotalCost < RecommendedMaximum) { //if-else statements to compare values
System.out.println("The total cost of...");
output.println("The total cost of...");
} else if (TotalCost == RecommendedMaximum) {
System.out.println("The total cost of...");
output.println("The total cost of...");
} else {
System.out.println("The total cost of...");
}
}
output.close(); //Close both Scanner and Printwriter
input.close();
}
}
我得到的错误是这样的:
Exception in thread "main" java.util.InputMismatchException
at java.util.Scanner.throwFor(Unknown Source)
at java.util.Scanner.next(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at java.util.Scanner.nextInt(Unknown Source)
at (package).WageCalculator.main(WageCalculator.java:30)
该错误表明问题出在我的代码中的第 30 行,但我不太确定。
如果有人需要查看 TestData.txt 文件:
Monarch //ShipID
M141 //JourneyID
16 //JourneyLength
6 //Crew
10.5 //RateOfPay -
10.5
20
20
20
30 //- RateOfPay
Princess //ShipID
P103 //JourneyID
18 //JourneyLength
5 //Crew
40 //RateOfPay -
45
45
60
80 //- RateOfPay
如有任何帮助,我们将不胜感激:)
最佳答案
在仅检查 input.hasNext()
一次后,您将在循环内进行大量 input.nextXXX()
调用循环的顶部。不要这样做,因为这是非常不安全并且必然会失败,而且 check 和 get 之间应该始终存在一对一的对应关系。例如,如果您想获取下一行,则在调用 input.nextLine()
之前应该调用一个 input.HasNextLine()
。 input.next()
或 input.nextInt()
相同。
我自己,我会通过检查 hasNextLine()
逐行读取,然后在 nextLine()
中一次读取,然后操作接收到的字符串。
while (input.hasNextLine()) {
String line = input.nextLine();
// now do not use input further within the loop
}
然后,您可以在循环中使用接收到的行使用第二个扫描仪,或通过 String#split(String regex) 拆分该行或执行您需要对其执行的任何操作。
或者您可以使用 String replaceAll(...)
和正则表达式来删除所有空格,后跟“//”,后跟任何字符。例如,
while (input.hasNextLine()) {
String line = input.nextLine();
// get rid of white space followed by "//" followed by anything
line = line.replaceAll("\\s+//.*", "");
System.out.println(line);
}
<小时/>
编辑
我对你的问题和数据进行了更多研究,我将修改我的答案。 如果您绝对确定数据文件的完整性,您可以考虑在每次获取整艘船的数据时检查一次下一行。例如:
public static void main(String[] args) {
InputStream inStream = GetData.class.getResourceAsStream(DATA_FILE);
List<Cruise> cruiseList = new ArrayList<>();
Scanner input = new Scanner(inStream);
while (input.hasNext()) {
String name = getLine(input);
String id = getLine(input);
int length = Integer.parseInt(getLine(input));
int crew = Integer.parseInt(getLine(input));
// assuming a class called Cruise
Cruise cruise = new Cruise(name, id, length, crew);
for (int i = 0; i < crew; i++) {
cruise.addPayRate(Double.parseDouble(getLine(input)));
}
cruiseList.add(cruise);
}
input.close();
}
private static String getLine(Scanner input) {
String line = input.nextLine();
// get rid of white space followed by "//" followed by anything
line = line.replaceAll("\\s+//.*", "");
return line;
}
关于java - While 循环与 hasNext();没有完全循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27844780/