简体   繁体   English

不使用Integer.ParseInt()进行转换

[英]No conversion with Integer.ParseInt()

I have a problem that when I use Integer.parseInt in this context it doesn't convert my and somehow it even kick me out of loop so it dont't want to display for example System.out.print(1) after loop like everything was crashed but i have no error. 我有一个问题,当我在这种情况下使用Integer.parseInt时,它不会转换我的内容,并且以某种方式甚至将我踢出循环,所以它不想在循环后显示例如System.out.print(1)一切都崩溃了,但我没有错误。 Please help. 请帮忙。 That's a part of code which cause it. 那是导致它的代码的一部分。 variable "input" is an Arrayl 变量“输入”是一个Arrayl

for (int i=0;i<input.size();i++) 
{
    if(point>Integer.parseInt(input.get(i).split(":")[1]))
    {    
        input.set(i,highScore + System.getProperty("line.separator"));
        break;
    }               
} 

Have you verified that input.get(i).split(":")[1] gives you exactly a string that only contains digits? 您是否已验证input.get(i).split(“:”)[1]确实为您提供了仅包含数字的字符串?

Integer.parseInt(String s) throws NumberFormatException, so you should execute that code inside a try/catch block like this: Integer.parseInt(String s)引发NumberFormatException,因此您应在try / catch块内执行该代码,如下所示:

for (int i=0;i<input.size();i++) {
  try {
    int parsedValue = Integer.parseInt(input.get(i).split(":")[1]);
    // do whatever you want to do with parsedValue
  }
  catch(NumberFormatException e) {
    System.out.print("I caught an error!, what i was trying to parse wasn't a number");
    // or any other action you consideer that needs to be done when parseInt fails
  }
}

There is only one explanation (if you are sure that no exception is thrown): the input is empty. 只有一种解释(如果您确定不会引发异常): input为空。 :) :)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM