while 循环与 hasNext();没有完全循环

While loop with hasNext(); not fully looping

我正在创建一个程序,该程序从外部文件中读取数据,将其与文件中的其他数据进行比较,然后将结果打印到新的外部文件中。我的代码的 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) 拆分行或做任何您需要做的事情。

或者您可以使用字符串 replaceAll(...) 和正则表达式来删除所有白色 space 后跟“//”后跟任何字符。例如,

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

它给你带来麻烦的原因是因为当用户输入一个整数然后按回车键时,刚刚输入了两件事 - 整数和一个 "newline" 即 \n。您正在调用的方法 "nextInt" 只读入整数,这会在输入流中留下换行符。但是调用 nextLine() 确实会读入换行符,这就是为什么您必须在代码运行之前调用 nextLine() 的原因。您也可以调用 next(),它也会读入换行符。

另请阅读扫描仪文档 class 以进一步了解:

这是更正后的代码:

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.Scanner;

import javax.swing.JOptionPane;

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
            System.out.println("While Enter"); // For debugging purpose
            ShipID = input.nextLine();
            JourneyID = input.nextLine();
            JourneyLength = input.nextInt();
            input.nextLine();     // Enter this to read the data of skipped Line
            Crew = input.nextInt();
            input.nextLine();
            CrewCost = 0; //Default Values Set
            TotalCost = 0;
            for (int x = Crew; x > 0; x--) { //For Loop updates the above values
                System.out.println("While Under if Enter");// For debugging purpose
                RateOfPay = input.nextDouble();
                input.nextLine();
                CrewCost = RateOfPay * JourneyLength;
                TotalCost = TotalCost + CrewCost;
                System.out.println("While Under if Exit");// For debugging purpose
            }

            if (TotalCost < RecommendedMaximum) { //if-else statements to compare values
                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...");
            }
            System.out.println("While Exit"); // For debugging purpose
        }    
        output.close(); //Close both Scanner and Printwriter
        input.close();
    }

}