【问题标题】:NumberFormatException with File IO带有文件 IO 的 NumberFormatException
【发布时间】:2015-11-26 00:51:27
【问题描述】:

我不断收到 NumberFormatException,据我所知,这是由于字符串转换引起的。我正在使用 Double.parseDouble 将 st.nexttoken 转换为 double。 任何帮助将不胜感激!

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:对于输入字符串:“输出”

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

output.txt 为空白,但保存在与 numbers.txt 相同的文件路径中

这是按要求提供的 numbers.txt 正文。 13 12 15 3

74.4 67.3 43.8 77.7 233.4 678.9

【问题讨论】:

  • 在转换之前尝试打印我们的token,看看token是否真的是一个数字
  • 您能在问题中发布 numbers.txt 吗?你可能有一个词“输出”里面。顺便说一句,您永远不会关闭输入文件。
  • 把numbers.txt的内容贴出来,不然大家只能提供调试建议了。
  • 我在底部添加了 numbers.txt。我查了一下,里面没有Output这个词。
  • 这里是一个 e.printStackTrace() java.lang.NumberFormatException:对于输入字符串:“输出”在 sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) 在 sun.misc.FloatingDecimal .parseDouble(FloatingDecimal.java:110) 在 java.lang.Double.parseDouble(Double.java:538) 在 FileIO.main(FileIO.java:27)

标签: java file-io numberformatexception


【解决方案1】:
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()将其写入最终目的地。

这是数字.txt

12 12.0 12
12 32 56
34
34.0

【讨论】:

  • 您仍然收到 NumberFormatException 吗?
  • 您仍然收到 NumberFormatException 吗?如果您遇到与您提到的相同的错误,那么您的文件中可能包含一些字符串。检查您是否引用了您提到的同一文件。我通过引用错误的文件做了很多次。为避免混淆,请使用完整路径和测试。只是为了确保。
猜你喜欢
  • 2012-11-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-09
  • 2021-12-15
  • 2011-09-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多