gpt4 book ai didi

java - 解析数字抛出 : Exception in thread "main" java. lang.NumberFormatException: 对于输入字符串

转载 作者:行者123 更新时间:2023-12-01 11:47:49 25 4
gpt4 key购买 nike

我正在制作一个面向对象的程序,它采用对象类型Date的数组并将其写入文件。但我在解析数字时遇到问题。我看不到我的 intString 变量在哪里混淆了:

public static Date[] createMe() throws FileNotFoundException
{
Scanner kb = new Scanner(System.in);
System.out.print("please enter the name of the file: ");
String fileName= kb.nextLine();
Scanner fin = new Scanner(new File(fileName));
int count = 0;
while (fin.hasNextLine()){
count++;
fin.nextLine();
}
fin.close();
Date[] temp = new Date[count];
fin = new Scanner(fileName);
while(fin.hasNextLine()){
String line = fin.nextLine();
String[] s = line.split("/");
int month = Integer.parseInt(s[0]);
int day = Integer.parseInt(s[1]);
int year = Integer.parseInt(s[2]);
}
return temp;
}

我不断收到此错误代码,但我不知道为什么:

please enter the name of the file: Track.java
Exception in thread "main" java.lang.NumberFormatException: For input string: "Track.java"
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 Date.createMe(Date.java:49)
at DateTester.main(DateTester.java:28)

我应该只使用我的 Scanner kb 输入字符串,对吧?那么为什么我会收到此错误?

最佳答案

问题出在这一行:

fin = new Scanner(fileName);

您正在从字符串文件名创建扫描程序。这是您输入的文件的路径。而不是文件本身。您在上面几行正确创建了 fin Scanner 。再做一次同样的事情。

关于java - 解析数字抛出 : Exception in thread "main" java. lang.NumberFormatException: 对于输入字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29019786/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com