Durnehviir
Durnehviir

Reputation: 66

When I type in something from my array into the Scanner, it is not doing the correct function

When I type in Beef, Pork, Chicken, or Cheeseburger, it should say "Here is your" and then whatever I typed in, while filtering out anything not in the array. It isn't working. Any help? (Yes I am new to Java) Fixed as best I could from feedback, still not working.

 import java.util.Scanner;
    public class scannerReview {
    public static void main(String[] args){
        String[] array = new String[]{"Beef","Pork","Chicken","Cheeseburger"};
        Scanner sc = new Scanner(System.in);
        System.out.println("What do you want for dinner? Your options are Beef, Pork, Chicken, and Cheesebugers. ");
        String food = sc.nextLine();
        if(!food.equals(array[0])){
            System.out.println("You cannot order that!");
        }
        else if(!food.equals(array[1])){
            System.out.println("You cannot order that!");
        }
        else if(!food.equals(array[2])){
            System.out.println("You cannot order that!");
        }
        else if(!food.equals(array[3])){
            System.out.println("You cannot order that!");
        }
        else{
            System.out.println("Here is your" + food);
        }
    }
    }

Console:

What do you want for dinner? Your options are Beef, Pork, Chicken, and Cheesebugers. 
Beef(My input)
You cannot order that!

Upvotes: 0

Views: 54

Answers (5)

Visruth
Visruth

Reputation: 3450

Here is the corrected code

import java.util.Scanner;
    public class scannerReview {
    public static void main(String[] args){
        String[] array = new String[]{"Beef","Pork","Chicken","Cheeseburger"};
        Scanner sc = new Scanner(System.in);
        System.out.println("What do you want for dinner? Your options are Beef, Pork, Chicken, and Cheesebugers. ");
        String food = sc.nextLine();
        if(food.equals(array[0])){
            System.out.println("Here is your" + food);
        } else if(food.equals(array[1])){
            System.out.println("Here is your" + food);
        } else if(food.equals(array[2])){
            System.out.println("Here is your" + food);
        } else if(food.equals(array[3])){
            System.out.println("Here is your" + food);
        } else {
            System.out.println("You cannot order that!");
        }
    }
    }

Upvotes: 0

takendarkk
takendarkk

Reputation: 3442

You can shorten this code a lot by using something like this:

if (Arrays.asList(array).contains(food)) { 
    System.out.println("Here is your " + food);
}
else { 
    System.out.println("You cannot order that!");
}

Also, it is good practice to use the equals() method to compare Strings instead of == which checks for object equality.

if (food.equals(array[1])) { ... }

Upvotes: 1

Mureinik
Mureinik

Reputation: 312086

You are comparing Strings with the != operator, which means your're checking if they are the exact same instance in memory - which probably won't happen if you're inputting data from the console.

Instead, you should use !food.equals(array[1]) to compare that the Strings are not equal (as opposed to being the same instance).

Also, note that arrays in Java are zero based - the first element is in index 0, the second in index 1 and so on.

Upvotes: 0

Rakesh KR
Rakesh KR

Reputation: 6527

index of array stars from 0 not from 1

Your array contains 4 elements ie array[0] to array[3]

Upvotes: 2

blackpanther
blackpanther

Reputation: 11486

Use !equals() instead of != for comparing Strings in Java. Remember, == and != is used to determine whether two object references point to the same object. equals() determines whether, in this case, whether two Strings are identical in terms of their character streams.

Upvotes: 1

Related Questions