Jeremy B
Jeremy B

Reputation: 863

String input in program

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?

Upvotes: 0

Views: 123

Answers (3)

Chandra Sekhar
Chandra Sekhar

Reputation: 19492

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
}

Upvotes: 2

Thomas
Thomas

Reputation: 5094

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

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500065

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"))
...

Upvotes: 7

Related Questions