Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java, how to compare Strings with String Arrays

I have been searching here for some time but haven't been able to find the answer to it.

I am basically required to use an array for this assignment from college. And then I am supposed to check that the input (which is also a String) matches whatever's stored within the String array.

I know one can easily compare Strings by using the .equals() method. However, the same method is not working with the String array.

I created the following example of code for the purpose of StackOverflow so you can use it to explain it to me, if you'd like.

What am I doing wrong?

import java.util.Scanner;

class IdiocyCentral {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        /*Prints out the welcome message at the top of the screen*/
        System.out.printf("%55s", "**WELCOME TO IDIOCY CENTRAL**\n");
        System.out.printf("%55s", "=================================\n");

        String [] codes = {"G22", "K13", "I30", "S20"};

        System.out.printf("%5s%5s%5s%5s\n", codes[0], codes[1], codes[2], codes[3]);
        System.out.printf("Enter one of the above!\n");

        String usercode = in.nextLine();

        if (codes.equals(usercode)) {
            System.out.printf("What's the matter with you?\n");
        }
        else {
            System.out.printf("Youda man!");
        }

    }
}

I apologize if this has been asked before and I just missed it, if its a double question, I will remove it.

like image 200
Nico Avatar asked Jan 20 '12 02:01

Nico


People also ask

Can we compare string with array?

You can compare string arrays and character vectors with relational operators and with the strcmp function. You can sort string arrays using the sort function, just as you would sort arrays of any other type.

How can I compare two strings in an array in Java?

Using Arrays. equals(array1, array2) methods − This method iterates over each value of an array and compare using equals method. Using Arrays. deepEquals(array1, array2) methods − This method iterates over each value of an array and deep compare using any overridden equals method.

Can you use == to compare strings in Java?

You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.

How do you compare two elements in a string array?

equals() Method. Java Arrays class provides the equals() method to compare two arrays. It iterates over each value of an array and compares the elements using the equals() method.


1 Answers

I presume you are wanting to check if the array contains a certain value, yes? If so, use the contains method.

if(Arrays.asList(codes).contains(userCode))
like image 78
Peter Olson Avatar answered Oct 01 '22 03:10

Peter Olson