Bib*_*ael 6 java string parsing file numberformatexception
我有一个String,我想从中解析一个整数,并且找不到超出此运行时异常的方法.我理解它有时会显示一个parseNUMBERTYPE函数应用于一个不恰当定义的String,并且代码期望数字的空格或字母可以触发它.但是,我用作测试虚拟的字符串就我简单地说数字5.我已经看到了几个建议,以响应其他用户的NumberFormatException问题,主张在解析之前应用trim()函数,我尝试过这个没有成功.
我也尝试用简单的,未存储的值"5"替换我想要解析的String.这与程序似乎报告为相关变量的存储字符串值相同,但在解析该变量时,由于此帖子的同名异常,未存储的值似乎在其位置上运行得非常好.
请注意,String变量由文件扫描程序读取.我必须假设我的问题与未知的,不需要的,"不可见的"字符有关,这些字符除了第五个之外还被读取,但我无法确定为什么会发生这种情况或如何阻止它.该文件格式为.txt
这是我的代码:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename = scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign to the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added later to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println(line1);
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem may in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1);
Run Code Online (Sandbox Code Playgroud)
我遇到了这些错误:
Exception in thread "main" java.lang.NumberFormatException: For input string: "5"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:492)
at java.lang.Integer.parseInt(Integer.java:527)
at Driver.main(Driver.java:26)
Run Code Online (Sandbox Code Playgroud)
非常感谢您的协助.
针对目前给出的建议,代码已经修改为如下所示:
Scanner scan = new Scanner(System.in);
System.out.println("Please enter the file name:");
String filename=scan.nextLine();
File tempfile = new File(filename);
Scanner filereader = new Scanner(tempfile);
//this is meant to assign the String line1 the numerical value (5)
//of the file's first line
String line1 = filereader.nextLine();
//this was added after to determine if line1 held the value I expect
//it outputs the single digit 5, as expected and appropriate
System.out.println("["+line1+"]");
//this was added to test how flawed my system was
//it runs fine, indicating to me that the problem is in my reader
int num2 = Integer.parseInt("5");
//this is where the exception is cast
int num1 = Integer.parseInt(line1.trim());
Run Code Online (Sandbox Code Playgroud)
如果我以某种方式误解了你的指导,请纠正我.就目前而言,问题仍然存在,并出现相同的错误报告.
查看异常中的消息,它表明字符串中没有其他可见字符或空格,因为 5 被引号包围(并且快速检查 Java 源代码表明这里打印的消息似乎不是修改为在用引号包围字符串之前删除空格)。
这意味着字符串中存在隐藏的非打印字符,或者 5 本身实际上根本不是 5,而是来自另一种语言的类似于 5 的某种 unicode 字符。
解决这个问题的简单调试案例是打印字符串的长度,因为这将快速找出其中是否有其他隐藏字符。
System.out.println("String: [" + line1 + "] size: " + line1.size());
Run Code Online (Sandbox Code Playgroud)
之后,可以使用正则表达式来获取第一组连续数字(如果这是所需的行为):
Pattern pattern = Pattern.compile("(\\d+)");
Matcher matcher = pattern.matcher(line1);
if (matcher.find())
{
String digits = matcher.group();
int num = Integer.parseInt(digits);
}
Run Code Online (Sandbox Code Playgroud)
或者,如果可以或希望删除数字之间的字符,则replaceAll可以使用 a:
String digits = line1.replaceAll("[^0-9]","");
Run Code Online (Sandbox Code Playgroud)
对于字符串“5 6”,第一个方法将为您提供 5,第二个方法将为您提供 56。