java parseint null_Integer.parseInt错误:java.lang.Integer.parseInt(未知来源)(Integer.parseInt Error : java...

Integer.parseInt错误:java.lang.Integer.parseInt(未知来源)(Integer.parseInt Error : java.lang.Integer.parseInt(Unknown Source))

我正在开发一个Java项目,将每个Integer添加到下一行中的那个,直到文件中没有要读取的行。 所以为了能够添加它我必须使用Integer.parseInt(...)到该行然后添加它。 PS:for循环只会跳过包含文件头的两行。 并且所有字符串都引用了Integer.parseInt()接受的数字。

这是完整的异常错误:

Exception in thread "main" java.lang.NumberFormatException: null

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at prog.Result(prog.java:93)

at prog.main(prog.java:56)

导致异常的代码是:

public static void Result() throws IOException

{

FileReader fileReader = new FileReader(dir+"/"+log_file);

BufferedReader bufferedReader = new BufferedReader(fileReader);

int i;

for (i=0;i<=2;++i)

{

bufferedReader.readLine();

}

int result =0;

while (bufferedReader.readLine() != null)

{

result += Integer.parseInt(bufferedReader.readLine());

}

System.out.println("The Result Is : " + result);

}

I'm working on a Java project to Add each Integer to the one in the next line until there's no lines to read in the file. So to be able to add it I have to use Integer.parseInt(...) to the line then add it. P.S : the for loop will just skip two lines which contain the header of the file. And all the string are refering to numbers which Integer.parseInt() accepts.

Here's the full exception error :

Exception in thread "main" java.lang.NumberFormatException: null

at java.lang.Integer.parseInt(Unknown Source)

at java.lang.Integer.parseInt(Unknown Source)

at prog.Result(prog.java:93)

at prog.main(prog.java:56)

The code resulting in the exception is :

public static void Result() throws IOException

{

FileReader fileReader = new FileReader(dir+"/"+log_file);

BufferedReader bufferedReader = new BufferedReader(fileReader);

int i;

for (i=0;i<=2;++i)

{

bufferedReader.readLine();

}

int result =0;

while (bufferedReader.readLine() != null)

{

result += Integer.parseInt(bufferedReader.readLine());

}

System.out.println("The Result Is : " + result);

}

原文:https://stackoverflow.com/questions/18777522

更新时间:2019-12-11 01:35

最满意答案

这个块实际上是读两行而不是一行。

while (bufferedReader.readLine() != null)

{

result += Integer.parseInt(bufferedReader.readLine());

}

在while条件检查中读取最后一行时发生错误,然后块的内部部分将读为null ,因为没有更多行要读取。

写这样的循环是不恰当的:

String line;

while ((line = bufferedReader.readLine()) != null)

{

result += Integer.parseInt(line);

}

This block is actually reading two lines, not one.

while (bufferedReader.readLine() != null)

{

result += Integer.parseInt(bufferedReader.readLine());

}

The error occurs when the last line is read in the while condition check, and the inner part of the block will then read null, since there are no more lines to be read.

It's idiomatic to write loops like this as:

String line;

while ((line = bufferedReader.readLine()) != null)

{

result += Integer.parseInt(line);

}

2013-09-13

相关问答

结果[0]的数据类型是什么? 如果它是一个字符串,你确定它周围没有空格或新行吗? 试试result[0].trim() What is the data type of result[0]? If it's a string, are you sure there are no spaces or new lines around it? Try result[0].trim()

看起来你每次迭代都在创建一个新数组,而不是添加它。 大概你的代码看起来像这样: for (int i = 0; i < coord.length; i++)

{

formeCoord = new int[coord.length];

formeCoord[i] = Integer.parseInt(coord[i]);

}

您需要将其更改为: formeCoord = new int[coord.length];

for (int i = 0; i < coord.length; i++

...

从Java 8开始,您可以使用Integer.parseUnsignedInt(s, 2); : System.out.println(Integer.parseUnsignedInt(Integer.toBinaryString(-1), 2));

Since Java 8 you can use Integer.parseUnsignedInt(s, 2);: System.out.println(Integer.parseUnsignedInt(Integer.toBinaryString

...

这个块实际上是读两行而不是一行。 while (bufferedReader.readLine() != null)

{

result += Integer.parseInt(bufferedReader.readLine());

}

在while条件检查中读取最后一行时发生错误,然后块的内部部分将读为null ,因为没有更多行要读取。 写这样的循环是不恰当的: String line;

while ((line = bufferedReader.readLine()) != null)

...

确保你正在用适当的编码构建一个阅读器。 你的代码应该是这样的: BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream("data.csv"), encoding));

String line;

while ((line = in.readLine()) != null) {

StringTokenizer tokenizer = new StringT

...

这是因为in.nextInt()之后扫描仪的位置; 将在输入整数之后。 in.nextLine(); 然后循环内部将不读任何内容。 解决方案 1)将扫描仪的位置设置为下一个新线 t= Integer.parseInt(in.nextLine());

2)有一个新的扫描仪对象 Scanner in2 = new Scanner(System.in);

String input=in2.nextLine();

This is because the position of scanner afte

...

已经在这里回答了Java。 不幸的是,scala AFAIK中的转化没有额外的治疗方法。 Scala在RichInt定义: def toHexString: String = java.lang.Integer.toHexString(self)

并在StringLike : def toInt: Int = java.lang.Integer.parseInt(toString)

除了import java.lang.{Long => JLong}并使用JLong我不知道比你

...

Delete 。 定位标记发出GET请求,而不是POST 。 你的请求在doGet()结束 Delete . anchor tags make a GET request, not a POST. your request is endin

...

尝试在没有Integer.parseInt()方法的情况下使用packedQtyVal = ${locPackedQtyValue} 。 它适用于BeanShell和BSF处理器中的Jmeter 2.11。 Try to use just packedQtyVal = ${locPackedQtyValue} without Integer.parseInt() method. It works for me in Jmeter 2.11 in BeanShell and BSF processo

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值