Question

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.

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.

Was it helpful?

Solution

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

strMonth = Integer.parseInt(month);

With the following:

month = Integer.parseInt(strMonth);

OTHER TIPS

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

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

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

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

try

strMonth = String.valueOf(month);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top