简体   繁体   English

线程“主”中的异常NumberFormatException

[英]Exception in thread “main” NumberFormatException

Exception in thread "main" java.lang.NumberFormatException: For input string: "1001" 线程“主”中的异常java.lang.NumberFormatException:对于输入字符串:“ 1001”

I dont understand why this code throws exception. 我不明白为什么这段代码会引发异常。

1001 elma 87 --> This is the text file and I'm sure this is a number. 1001 elma 87 >这是文本文件,我确定这是一个数字。

public class Food {
    public String[][] foodArray = new String[1000][100];
    public int sayac = 0;

    public void readText() {
      try (Scanner sc = new Scanner(new BufferedReader(new FileReader("food.txt")))) {
        while (sc.hasNextLine()) {
            String bilgiler = sc.nextLine().trim();
            foodArray[sayac] = bilgiler.split("\t");
            System.out.println(Integer.parseInt(foodArray[sayac][0]));
            sayac++;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}

} }

Exception in thread "main" java.lang.NumberFormatException: For input string: "1001"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:580)
at java.lang.Integer.parseInt(Integer.java:615)
at Food.readText(Food.java:13)
at Main.main(Main.java:11)

The only problem that I can see that in the file the character that is separating the words is not a tab (\\t) character, but maybe just spaces. 我可以看到的唯一问题是,文件中分隔单词的字符不是制表符(\\ t),而是空格。 That's why it is not able to split the line correctly. 这就是为什么它不能正确split线的原因。 Can you show us what the exact stack trace is? 您能告诉我们确切的堆栈跟踪是什么吗?

Delimiting character is not tab(\\t) which is why the line is not getting split in different parts and whole line is stored at 0th index of the array. 分隔符不是tab(\\ t),这就是为什么行没有分成不同的部分,并且整行存储在数组的第0个索引处的原因。 And when you use parseInt it tries to parse the whole line which is 1001 elma 87 当您使用parseInt时,它将尝试解析整行,即1001 elma 87

I would suggest using bilgiler.split(" ") which will take care of single or multiple spaces used as delimiter. 我建议使用bilgiler.split(" ") ,它将处理用作分隔符的单个或多个空格。

your file line separator might not be tabs, i advise you to print the split array and use bilgiler.split("\\\\s+"); 您的文件行分隔符可能不是制表符,我建议您打印拆分数组并使用bilgiler.split("\\\\s+"); to split all the spaces between words 分隔单词之间的所有空格

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 线程“main”java.lang.NumberFormatException中的异常: - Exception in thread “main” java.lang.NumberFormatException: 线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“ 0.06” - Exception in thread “main” java.lang.NumberFormatException: For input string: “0.06” java-线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“”位于 - java - Exception in thread “main” java.lang.NumberFormatException: For input string: “” at 线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“8/3/2012” - Exception in thread "main" java.lang.NumberFormatException: For input string: "8/3/2012" 线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:Java中的“” - Exception in thread “main” java.lang.NumberFormatException: For input string: “” in Java 线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“” - Exception in thread “main” java.lang.NumberFormatException: For input string: “” 线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“0.353” - Exception in thread “main” java.lang.NumberFormatException: For input string: “0.353” 线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串 - Exception in thread “main” java.lang.NumberFormatException: For input string 线程“main”中的异常 java.lang.NumberFormatException:对于输入字符串:“38” - Exception in thread "main" java.lang.NumberFormatException: For input string: " 38" 线程“ main”中的异常java.lang.NumberFormatException:对于输入字符串:“” Array - Exception in thread “main” java.lang.NumberFormatException: For input string: “” Array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM