Search code examples
javaparseint

Java thinks I want to convert char to String using Integer.parseInt()


I get the error message error: incompatible types: char cannot be converted to String return Integer.parseInt(cases2[0]);

but my code is:

public static int standingOvation(String cases){

    char[] cases2 = cases.toCharArray();
    return Integer.parseInt(cases2[0]);
}

Why am I getting the error if i'm clearly trying to convert cases (which is passed in as "11111") to an integer?


Solution

  • Try

    return Integer.parseInt("" + cases2[0]);
    

    By adding to the empty string "" you convert to a string, which is the right type for Integer.parseInt.