Reputation: 863
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
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
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