Java解析整数抛出错误

Java解析整数抛出错误,java,parseint,Java,Parseint,我的tester类中有以下代码片段 FileReader freader=new FileReader(filename); BufferedReader inputFile=new BufferedReader(freader); int numScores = 0; String playerType = ""; String nameHome = ""; String playerName = ""; String ho

我的tester类中有以下代码片段

     FileReader freader=new FileReader(filename);
     BufferedReader inputFile=new BufferedReader(freader);
     int numScores = 0;
     String playerType = "";
     String nameHome = "";
     String playerName = "";
     String home = "";
     String location = "";
     int score = 0;
     String date = "";
     double courseRating = 0;
     int courseSlope = 0;

     ArrayList<Player> players = new ArrayList<Player>();

     while (inputFile.read()!= -1) {
        numScores = Integer.parseInt(inputFile.readLine()); 
        playerType = inputFile.readLine();
        nameHome = inputFile.readLine();
        StringTokenizer st = new StringTokenizer(nameHome,",");
        playerName = st.nextToken();
        home = st.nextToken();
我试着研究这个,我发现当它改变从数据文件读取的字符串并将其转换为int时,可能会有一个空格。我试着直接读取到strin,修剪字符串,然后转换为int,但它得到了相同的错误

这是替换numcores=Integer.parseInt(inputFile.readLine())的代码

感谢您的帮助

*编辑以显示示例数据 文件中的示例数据

3
B
James Smith, Strikers
FWB Bowling, 112,09/22/2012
White Sands, 142,09/24/2012
Cordova Lanes,203,09/24/2012

您可以将其全部放在
if
语句中:

if(!tempScores.equalsIgnoreCase(""))
{

您可以将其全部放在
if
语句中:

if(!tempScores.equalsIgnoreCase(""))
{

您的文件可能包含空行。它们被读取为“”,因此无法转换为int

此外,还可以通过while循环头中的read语句读取每行的第一个字符,以便在readline命令中忽略它。然后,长度为1的数字(如“1”)将成为一条空行


在任何情况下,循环的构造都是一个bug。

文件可能包含空行。它们被读取为“”,因此无法转换为int

此外,还可以通过while循环头中的read语句读取每行的第一个字符,以便在readline命令中忽略它。然后,长度为1的数字(如“1”)将成为一条空行

在任何情况下,循环的构造都是一个bug。

您需要理解,请查看它

基本认识是

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught.
} 
还有一个finally块,即使出现错误,它也将始终被执行。它是这样使用的

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught & the returned.
} finally {
  // This will always execute if there is an exception or no exception.
}
在您的特定情况下,您可以有以下例外()

InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围 NoTouchElementException-如果输入用尽 IllegalStateException-如果此扫描仪已关闭

因此,您需要捕获以下异常:

try { 
   rows=scan.nextInt();
} catch (InputMismatchException e) {
  // When the InputMismatchException is caught.
  System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
  // When the NoSuchElementException is caught.
  System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
  // When the IllegalStateException is caught.
  System.out.println("Scanner is close");
} 
你需要了解,请调查一下

基本认识是

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught.
} 
还有一个finally块,即使出现错误,它也将始终被执行。它是这样使用的

try { 
   //Something that can throw an exception.
} catch (Exception e) {
  // To do whatever when the exception is caught & the returned.
} finally {
  // This will always execute if there is an exception or no exception.
}
在您的特定情况下,您可以有以下例外()

InputMismatchException-如果下一个标记与整数正则表达式不匹配,或超出范围 NoTouchElementException-如果输入用尽 IllegalStateException-如果此扫描仪已关闭

因此,您需要捕获以下异常:

try { 
   rows=scan.nextInt();
} catch (InputMismatchException e) {
  // When the InputMismatchException is caught.
  System.out.println("The next token does not match the Integer regular expression, or is out of range");
} catch (NoSuchElementException e) {
  // When the NoSuchElementException is caught.
  System.out.println("Input is exhausted");
} catch (IllegalStateException e) {
  // When the IllegalStateException is caught.
  System.out.println("Scanner is close");
} 

我今天遇到了类似的问题。我正在读取REST端点的响应,并尝试解析json响应。砰!犯了一个错误。后来我意识到这个文件有一个BOM表

我的建议是创建一个var

String var = inputFile.readLine();
int numScores = Integer.parseInt(var);
添加一个断点并检查var包含什么,在我的例子中,响应有一个BOM,一个空的unicode字符代码65279/0xfeff。在任何值得一试的调试器中,您都应该能够看到每个字符

如果是这种情况,则需要从字符串中删除该值

我使用这个库来检测这个问题org.yaml:snakeyaml:1.16

import org.yaml.snakeyaml.reader.UnicodeReader;

//more code

  private String readStream(InputStream inputStream) throws IOException {
    UnicodeReader unicodeReader = new UnicodeReader(inputStream);
    char[] charBuffer = new char[BUFFER_SIZE];
    int read;
    StringBuilder buffer = new StringBuilder(BUFFER_SIZE);
    while ((read = unicodeReader.read(charBuffer,0,BUFFER_SIZE)) != -1) {
      buffer.append(charBuffer, 0, read);
    }
    return buffer.toString();
  }

我今天遇到了类似的问题。我正在读取REST端点的响应,并尝试解析json响应。砰!犯了一个错误。后来我意识到这个文件有一个BOM表

我的建议是创建一个var

String var = inputFile.readLine();
int numScores = Integer.parseInt(var);
添加一个断点并检查var包含什么,在我的例子中,响应有一个BOM,一个空的unicode字符代码65279/0xfeff。在任何值得一试的调试器中,您都应该能够看到每个字符

如果是这种情况,则需要从字符串中删除该值

我使用这个库来检测这个问题org.yaml:snakeyaml:1.16

import org.yaml.snakeyaml.reader.UnicodeReader;

//more code

  private String readStream(InputStream inputStream) throws IOException {
    UnicodeReader unicodeReader = new UnicodeReader(inputStream);
    char[] charBuffer = new char[BUFFER_SIZE];
    int read;
    StringBuilder buffer = new StringBuilder(BUFFER_SIZE);
    while ((read = unicodeReader.read(charBuffer,0,BUFFER_SIZE)) != -1) {
      buffer.append(charBuffer, 0, read);
    }
    return buffer.toString();
  }

很明显“”不是数字…请使用调试器。添加变量字符串并指定读取行的值。验证是一个实际数字。如果是,请验证每个字符。很明显“”不是数字…请使用调试器。添加变量字符串并指定读取行的值。验证是一个实际数字。如果为“验证每个字符”。此文件不包含空行,这些仅用于初始化变量。添加输出语句后,读取的第一行为空。在不删除第一行的情况下完成循环的更好方法是什么?请查看此文件不包含空行,这些只是初始化变量。添加输出语句后,读取的第一行是空的。在不删除第一行的情况下完成循环的更好方法是什么?请查看空字符串的equalsIgnoreCase?为什么不把常量放在左边以避免NPE?空字符串的equalsIgnoreCase?为什么不把常数放在左边以避免NPE呢?谢谢你们的投入。谢谢你们的投入。