为什么hasNextInt()在这里返回false?

时间:2015-07-07 20:03:04

标签: java java.util.scanner

这是我第一次使用Java扫描文件。我试图从一个文本文件中读取整数列表,对它们进行操作,然后将它们读取到另一个文件中。初始文件如下所示:

1234
9876
2345

我使用while循环并使用hasNextInt进行读取(在运行程序并检查初始文本文件之后,我确定值已存在),但我输出为零新的文本文件,所以我假设hasNextInt()返回false。可以肯定的是,我只是跳过扫描并使用for循环直接在每个ArrayList元素上操作,输出完全写入新文件。这是我的代码:

    try {
        File inFile = new File("input.txt");
        File outFile = new File("output.txt");

        FileWriter inWriter = new FileWriter(inFile);
        FileWriter outWriter = new FileWriter(outFile);
        Scanner inReader = new Scanner(inFile);

        numberArray.add(1234); //Adding numbers to an array that was previously initialized
        numberArray.add(9876);
        numberArray.add(2345);

        System.out.println("The raw numbers are:");

        for (int n : numberArray) {
            inWriter.write(n + "\r\n");
            System.out.println(n);
        }

        System.out.println("\nThe new numbers are:");

        while (inReader.hasNextInt()) {
            int newNumber = inReader.nextInt();
            int checkDigit = (newNumber / 7);
            int finalNumber = (newNumber % checkDigit);
            outWriter.write("" + newNumber + finalNumber + "\r\n");
            System.out.println("" + newNumber + finalNumber);
        }

        inWriter.close();
        outWriter.close();
        inReader.close();

    } catch (FileNotFoundException fnfe) {
        System.out.println("There was a problem with the inFile file");
    } catch (IOException ioe) {
        System.out.println("There was a problem opening the outFile file");
    }

3 个答案:

答案 0 :(得分:6)

您必须在读取文件中的数字之前关闭inWriter。

inWriter不会将文本输出到文件,直到它关闭,所以你的读者看不到任何东西,因为技术上还没有任何东西。

答案 1 :(得分:1)

问题是您正在使用扫描程序解析文件。当您获取扫描仪前进的文件编号时,当您读取所有数字时,扫描仪到达终点并保持在那里.... 文件的末尾在那里大喊大叫还有更多要扫描的东西。

所以最后你应该使用数组中的数字存储,以避免再次重新扫描整个文件。

这对你有用吗?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;


public class Main {

  public static void main(String[] args) {
    try {
      ArrayList<Integer> numberArray = new ArrayList<>();
      File inFile = new File("input.txt");
      File outFile = new File("output.txt");

      FileWriter inWriter = new FileWriter(inFile);
      FileWriter outWriter = new FileWriter(outFile);
      Scanner inReader = new Scanner(inFile);

      numberArray.add(1234); // Adding numbers to an array that was previously initialized
      numberArray.add(9876);
      numberArray.add(2345);

      System.out.println("The raw numbers are:");

      for (int n : numberArray) {
        inWriter.write(n + "\r\n");
        System.out.println(n);
      }

      System.out.println("\nThe new account numbers are:");

      for (int newNumber : numberArray) { // re-use the array.. no need to scan the file again.
        int checkDigit = (newNumber / 7);
        int finalNumber = (newNumber % checkDigit);
        outWriter.write("" + newNumber + finalNumber + "\r\n");
        System.out.println("" + newNumber + finalNumber);
      }

      inWriter.close();
      outWriter.close();
      inReader.close();

    } catch (FileNotFoundException fnfe) {
      System.out.println("There was a problem with the inFile file");
    } catch (IOException ioe) {
      System.out.println("There was a problem opening the outFile file");
    }
  }
}

答案 2 :(得分:0)

在inReader流中再次使用之前,您没有刷新/关闭打字机流。