gpt4 book ai didi

java - Unresolved 编译问题: The method parseInt(String) is undefined for the type Integer

转载 作者:行者123 更新时间:2023-12-02 11:12:42 24 4
gpt4 key购买 nike

为什么不喜欢parseInt?如何解决?

import java.lang.*;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import java.lang.*;

public class Table {

static class Data
{
private String name = "";
private int num = 0;

public Data(String name, int num)
{
this.name = name;
this.num = num;
}

public String getName()
{
return name;
}

public int getNum()
{
return num;
}

public String toString()
{
return name + " " + num;
}
}



public static void main(String[] args) throws IOException
{

List<Data> table = new ArrayList<Data>();

try
{
String filename= ""C:\\input.txt";
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line = reader.readLine();

while(line != null)
{
String[] tokens = line.split("[ ]+");

String tempname = tokens[0];
int tempnum = Integer.parseInt(tokens[1]);

Data temp = new Data(tempname,tempnum);

table.add(temp);
line = reader.readLine();
}
reader.close();
}
catch(FileNotFoundException n)
{
System.out.println("file not found");
}
catch(IOException a)
{
a.printStackTrace();
}

for(Data n:table)
{
System.out.println(n);
}

}

}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
The method parseInt(String) is undefined for the type Integer

at Table.main(Table.java:58)

最佳答案

代码中有两个问题:

  • 删除导入的

  • import java.lang.*;


  • 路径周围的引号不正确
  • 我猜您对split()方法的正则表达式不正确。因此,假设您的输入文件具有以下格式:
    name[]number

    以下应该工作:
    public static void main(String[] args) throws IOException {

    List<Data> table = new ArrayList<Data>();

    try {
    String filename= "/home/kiflem/input.txt";
    BufferedReader reader = new BufferedReader(new FileReader(filename));
    String line = reader.readLine();

    while(line != null) {
    String[] tokens = line.split("\\[\\s*\\]+");
    if (tokens != null && tokens.length == 2) {
    String tempname = tokens[0];
    int tempnum = Integer.parseInt(tokens[1]);
    Data temp = new Data(tempname,tempnum);
    table.add(temp);
    }
    line = reader.readLine();
    }
    .............
  • 如果分隔符为空格,则使用

  • "\\s+"



    作为您的正则表达式。

    关于java - Unresolved 编译问题: The method parseInt(String) is undefined for the type Integer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36106588/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com