Java Integer.parseInt() 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18478148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:58:46  来源:igfitidea点击:

Integer.parseInt() Error

javaparseint

提问by Ethan Edwards

i'm getting an errors while trying to use Integer.parseInt(). They are, no suitable method found for parseInt(int) and method Integer.pasreInt(String) in not applicable.

我在尝试使用时遇到错误Integer.parseInt()。他们是,没有找到合适的方法 parseInt(int) 和方法 Integer.pasreInt(String) 不适用。

import java.io.InputStream; 
import java.util.Scanner; 
import java.util.regex.Pattern; 

class bday1
{ 
public static void main(String[] args) 
{ 
    Scanner sc = new Scanner(System.in);
    int day;
    int month=0;
    int year;
    int whatDay;
    int howManyDays=0;
    int leapYear;
    final int JANYEAR = 1901;
    int[] dayMonth = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    boolean numberEntered=true;
    String strMonth;
    String[] days = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};

    System.out.print("What is your birthdate? "); 
    sc.useDelimiter(Pattern.compile("[-/.\s]")); 
    day = sc.nextInt();
    strMonth = sc.next();
    year = sc.nextInt();

    if((strMonth.charAt(0) >='0') && (strMonth.charAt(0) <='9'))
    {
        numberEntered=true;
        System.out.println ("number entered");
    }

    if(numberEntered)
    {
        strMonth=Integer.parseInt(month);
    }

    else
    {
        System.out.println ("string entered");
    }

Thats my snippet of code i believe i having trouble with. Any help would be great.

这就是我的代码片段,我相信我遇到了麻烦。任何帮助都会很棒。

采纳答案by Decimix

Since month is initialized to zero and isn't changed afterwards, setting strMonthto monthwould only succeed in setting it to 0. By the looks of your code, it appears that you are trying to set the value of monthto strMonth. To do this, replace:

由于 month 初始化为零并且之后不会更改,因此设置strMonthtomonth只会成功将其设置为 0。从您的代码来看,您似乎正在尝试将值设置为monthto strMonth。为此,请替换:

strMonth = Integer.parseInt(month);

With the following:

具有以下内容:

month = Integer.parseInt(strMonth);

回答by newuser

You are trying to assign the String value into the int dataType. so only it wont allow you.

您正在尝试将 String 值分配给 int 数据类型。所以只有它不会允许你。

strMonth= Integer.parseInt(month); // variable month and strMonth is int dataType

And then the Integer.parseInt()will return the String value. Change the type to String or convert the String to int

然后Integer.parseInt()将返回字符串值。将类型更改为 String 或将 String 转换为 int

try

尝试

strMonth = String.valueOf(month);

回答by Scary Wombat

In you code you are use parseInt which expects a String. The month that you are using is already an int.

在您的代码中,您使用的是需要字符串的 parseInt。您使用的月份已经是一个整数。

What you are trying to do is convert an int to a String. Normal ways would be Integer.toString(i)or String.valueOf(i).

您要做的是将 int 转换为 String。正常方式是Integer.toString(i)String.valueOf(i)