string.startsWithIgnoreCase("") returns error?

4.2k views Asked by At

When I type in the following code into Eclipse:

Scanner keyboard = new Scanner (System.in);
System.out.print("Please input a word ");
String name = keyboard.nextLine();

if(input.startsWithIgnoreCase("pre"))
    {
        return input;
    }
    else
    {
        return "blah";
    }

It red-underlines the input.startsWithIgnoreCase("pre") part and returns the following error: "The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files".

I'm not sure why it returns this error or what it even means. What I want to do is check that the inputted word starts with "pre" while ignoring case.

2

There are 2 answers

0
Paraneetharan Saravanaperumal On

Try this code:

if(input.toLowerCase().startsWith("pre"))
0
TameHog On

I don't think that's an actual method that comes in the String class (correct me if I'm wrong). You would have to do something like this instead: input.toLowerCase().startsWith(string.toLowerCase()) In your case string would be "pre"