NumberFormatException parseInt Java
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18583842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
NumberFormatException parseInt Java
提问by user2398101
I am trying to differentiate and store data (Point: x, y) from a text file in two arraylists: knots and zeros. Data File just contains integers or decimal values.
我试图从两个数组列表中的文本文件中区分和存储数据(点:x,y):结和零。数据文件只包含整数或十进制值。
Data File:
x y type(zero/knot)
46 10 2
13 5 2
27 21 1
But my code is throwing NumberFormatException:48
但我的代码抛出 NumberFormatException:48
import java.util.*;
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;
public class program {
public static void main(String []args) {
ArrayList knots = new ArrayList<Point>();
ArrayList zeros = new ArrayList<Point>();
List<Integer> list = new ArrayList<Integer>();
String line = null;
String file = "data1.txt";
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine()) != null) {
String tmp[] = line.split(" ");
System.out.println(line);
for (String s:tmp) {
s = s.replace("\r\n","");
int i = Integer.parseInt(s.trim());
//knots.add(new Point();
System.out.println(s);
}
}
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
回答by Kon
You said you are parsing decimal numbers as well. Those cannot be parsed with Integer.parseInt()
. You need to use Double.parseDouble()
or Float.parseFloat()
to parse decimal values from a String.
你说你也在解析十进制数。那些不能用Integer.parseInt()
. 您需要使用Double.parseDouble()
或Float.parseFloat()
解析字符串中的十进制值。
Also, it looks like you are trying to parse your header in your file. Make sure to do a token readLine()
call before you begin the reading of the data, otherwise you'll try to parse text and that makes no sense and will crash.
此外,您似乎正在尝试解析文件中的标头。确保readLine()
在开始读取数据之前进行令牌调用,否则您将尝试解析文本,这毫无意义并且会崩溃。
回答by MadProgrammer
Assuming the file contents is EXACTLY...
假设文件内容完全是......
46 10 2
13 5 2
27 21 1
When I run you code, I get the output...
当我运行你的代码时,我得到了输出......
46 10 2
46
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:504)
at java.lang.Integer.parseInt(Integer.java:527)
at testparser.TestParser.main(TestParser.java:47)
This would seem to indicate that there are blank/empty Strings in the output...
这似乎表明输出中有空白/空字符串......
If I replace this section...
如果我替换此部分...
for (String s : tmp) {
s = s.replace("\r\n", "");
int i = Integer.parseInt(s.trim());
//knots.add(new Point();
System.out.println(s);
}
With...
和...
for (String s : tmp) {
s = s.trim();
if (s.length() > 0) {
int i = Integer.parseInt(s.trim());
//knots.add(new Point();
System.out.println(s);
}
}
I get...
我得到...
46 10 2
46
10
2
13 5 2
13
5
2
27 21 1
27
21
1
Don't forget, readline
will not return the line terminator, so you should be able to ignore it.
不要忘记,readline
不会返回行终止符,因此您应该可以忽略它。
line.split(" ")
is spliting on each "individual" space, meaning that if you have more than one space character together, then it will return the second space...;)
line.split(" ")
在每个“单个”空间上拆分,这意味着如果您有多个空格字符,那么它将返回第二个空格...;)
If, instead, you use String tmp[] = line.split("\\s+");
, it will split on any spaces between all the other characters...not returning any "blank" String
s (making the if (s.length() > 0) {
check redundant)
相反,如果您使用String tmp[] = line.split("\\s+");
,它将在所有其他字符之间的任何空格上拆分......不返回任何“空白” String
(使if (s.length() > 0) {
检查变得多余)
回答by Vimal Bera
The problem is in your split line. It should be like this String a[]=s.split(" +");
What happened in your case is : When it split "46 10 2" string with " "
, it counts only one space for spliting the String. So it ignores one space after 46
and 10
and then looks for the String. But there is no string after one space so it takes ""
as a string and try to parse it in integer. Though ""
can't be parsed into integer,it throws NumberFormatException
问题出在你的分割线上。它应该是这样的String a[]=s.split(" +");
你的情况发生了什么:当它用 分割“46 10 2”字符串时" "
,它只计算一个用于分割字符串的空间。因此,它忽略了一个空格后46
和10
,然后查找字符串。但是一个空格后没有字符串,所以它""
作为一个字符串并尝试以整数解析它。虽然""
无法解析为整数,但会抛出 NumberFormatException
回答by NSM
You can try this code.. It works fine for me
你可以试试这个代码..对我来说很好用
import java.util.ArrayList;
import java.io.*;
import java.awt.Point;
public class program
{
public static void main(String []args)
{
ArrayList knots = new ArrayList<Point>();
ArrayList zeros = new ArrayList<Point>();
List<Integer> list = new ArrayList<Integer>();
String line = null;
String file = "data1.txt";
BufferedReader reader;
try
{
reader = new BufferedReader(new FileReader(file));
while((line = reader.readLine()) != null)
{
String tmp[] = line.split(" ");
System.out.println(line);
for (String s:tmp)
{
if(! (s.isEmpty()) )
{
int i = Integer.parseInt(s.trim());
System.out.println(s);
}
}
}
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
}
}
I hope this will help you.
我希望这能帮到您。