Integer 类型未定义方法 parseInt(String) [重复]

问题描述 投票:0回答:1

我需要使用 Integer.parseInt() 或 Integer.valueOf() 将字符串转换为 int,但 Visual Studio Code 显示此错误未定义 Integer 类型的方法 parseInt(String)

到目前为止这是我的代码

import javax.swing.*;
import java.util.*;
public class entrada2 {

    public static void main(String[] args) {

        String nombre_usuario = JOptionPane.showInputDialog("Introduce tu nombre de usuario, por favor");
        String edad = JOptionPane.showInputDialog("Introduce tu edad, por favor");

        int age = Integer.parseInt(edad);

        System.out.println("Te llamas " + nombre_usuario + " y tienes " + edad + " años.");

    }


}

我不知道是什么问题。谷歌也没有帮助

显然 Integer.parseInt() 是在 Java 中从字符串生成整数的唯一方法,所以我被卡住了

java string integer
1个回答
-2
投票

此错误消息表明代码正在尝试使用 Integer 类的 parseInt 方法,但 Visual Studio Code 无法找到此方法。

parseInt 方法是 Integer 类的静态方法,这意味着它可以直接在类本身上调用,而不是在类的实例上调用。

要解决此错误,您可能需要确保在代码中导入了 Integer 类。您可以在文件开头添加以下导入语句:

导入java.lang.Integer;

© www.soinside.com 2019 - 2024. All rights reserved.