文件 IO 的 NumberFormatException

NumberFormatException with File IO

我不断收到 NumberFormatException,我知道这是由于字符串转换引起的。我正在将 st.nexttoken 转换为 Double.parseDouble 的双精度数。 任何帮助将不胜感激!

import java.io.*;
import java.util.*;

public class FileIO {

    public static void main(String[] args) throws Exception {
        double sum = 0, next ;
        int ctr = 0;
        String line;  
        String filename = "numbers.txt";
        StringTokenizer st;
        PrintWriter outFile = new PrintWriter("output.txt");
        outFile.println("Output File");
        try{
            Scanner inFile = new Scanner(new FileReader (filename));
            while (inFile.hasNext())
            {
                line = inFile.nextLine();
                st = new StringTokenizer(line);
                while (st.hasMoreTokens()){
                    next = Double.parseDouble(st.nextToken());
                    sum += next;
                    ctr++;
                    System.out.println(next);
                    outFile.println(next);
                }
            }
            System.out.println("number of doubles read is " + ctr);
            System.out.println("average is " + sum/(double)ctr);
            outFile.println("number of doubles read is " + ctr);
            outFile.println("average is " + sum/(double)ctr);
            outFile.close();
        }
        catch(FileNotFoundException e){
            System.out.println("The file numbers.txt was not found");
        }
        catch(NumberFormatException e){
            System.out.println("sorry - number format error");
            System.out.println(e);
        }
    }
}

来自 NetBeans 的错误显示为抱歉 - 数字格式错误 java.lang.NumberFormatException:对于输入字符串:"Output"

numbers.txt 有一些整数和带小数点的双精度数。

output.txt 为空白,但保存在与 numbers.txt

相同的文件路径中

这里是 numbers.txt 请求的正文。 13 12 15 3

74.4 67.3 43.8 77.7 233.4 678.9

public static void main(String[] args) throws Exception {
    double sum = 0, next ;
    int ctr = 0;
    String line;
    String filename = "numbers.txt";
    StringTokenizer st;
    PrintWriter outFile = new PrintWriter("output.txt");
    outFile.println("Output File");
    try{
        Scanner inFile = new Scanner(new FileReader(filename));
        while (inFile.hasNext())
        {
            line = inFile.nextLine();
            st = new StringTokenizer(line);
            while (st.hasMoreTokens()){
                next = Double.parseDouble(st.nextToken());
                sum += next;
                ctr++;
                System.out.println(next);
                outFile.println(next);
            }
        }
        System.out.println("number of doubles read is " + ctr);
        System.out.println("average is " + sum/(double)ctr);
        outFile.println("number of doubles read is " + ctr);
        outFile.println("average is " + sum/(double)ctr);
        outFile.flush();
        inFile.close();
        outFile.close();
    }
    catch(FileNotFoundException e){
        System.out.println("The file numbers.txt was not found");
    }
    catch(NumberFormatException e){
        System.out.println("sorry - number format error");
        System.out.println(e);
    }
}

以上是您的代码的修改版本,它适用于下面给定的 number.txt。

您需要使用 flush() 将其写入最终目的地。

这是number.txt

12 12.0 12
12 32 56
34
34.0