java - Unresolved 编译问题: The method parseInt(String

为什么不喜欢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/

    相关文章:

    c++ - 具有来自不同 header 的模板类的类将无法编译

    c++ - 代码块错误

    c# - 错误 'Cannot Convert double to int'

    c++ - 奇怪的错误: undefined reference to `class::class(

    c++ - C++构建错误: class does not name a type

    c++ - 尝试编译我的wxWidgets程序时出现 “is_enum not declared i

    c# - 并非所有代码路径都返回值: Unity

    java - BufferedReader错误

    java - 变量名称不能在Eclipse中解析为变量错误(多重继承)

    java - 编译错误: Can not convert boolean into an int