0

I can't find the answer in my book and it's an easy one but I'm missing something simple here.

I have a program where I ask the user "choose customer or employee C or E" Then after that I need to say if they picked C then do this or if they picked E do that. I'm new and struggling so I know this is probably an easy thing but I'm not getting it.

    Scanner in = new Scanner(System.in);
    system.out.println("choose c or e: ");

    if (in.equalsIgnoreCase("c"))
    {
    //do something
    }

    if (in.equalsIgnoreCase("e"))
    {
   // do something
    }

what am I doing wrong here?

0

3 Answers 3

7

The Scanner isn't the actual input - you need to read the input from the scanner, e.g.

Scanner scanner = new Scanner(System.in);
// Note capital S - Java is case-sensitive
System.out.println("choose c or e: ");

String input = scanner.nextLine();

if (input.equalsIgnoreCase("c"))
...
Sign up to request clarification or add additional context in comments.

Comments

2

Scanner has no method like equalsIgnoreCase(), this method is present in String. So using next() or nextLine() get a String and do what you want

Scanner in = new Scanner(System.in);
system.out.println("choose c or e: ");
String value = in.next();
if (value.equalsIgnoreCase("c"))
{
//do something
}

if (value.equalsIgnoreCase("e"))
{
// do something
}

Comments

1

in is a Scanner, not a string. Use in.next()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.