how can i compare a string input from a jtextfield (.getText()) with another String[] Array? +few conditions

StackOverflow https://stackoverflow.com/questions/19802748

  •  04-07-2022
  •  | 
  •  

Question

I need to do say that if the input(text field) is empty when the button is clikced then print a message. and if one of the characters in the input doesn't equal to one of the elements in 'legal' list then print another error message. if all the characters in the input are fine then add them to some list and print a message msg

    String[] legal={"a", "b", "c", "d" ,"e" ,"f" ,"g" ,"h" ,"i" ,"j" ,"k" ,"l" ,"m" 
 ,"n","o","p","q","r","s","t","u","w","x","y","z","-","_"}; 

    if(e.getSource() == add){

        if (txt.getText().equals("")){

            content.removeAll();
            content.add(empty);           
            content.revalidate();

        }  
        String[] splited = txt.getText().toLowerCase().split("");
        for (int t=0;t<splited.length;t++) {
            for (int u=0;u<legal.length;u++){
                if(splited[t] != legal[u]){

                    content.removeAll();
                    content.add(check);
                    content.revalidate();
                }
                else if(splited[t].equals( legal[u])){

                    content.removeAll();
                    list.add(txt.getText());
                    content.add(msg);
                    content.revalidate();

                }
            }
        }
    }
Was it helpful?

Solution

Let's take the UI out of the equation for moment.

To test if a String is empty, you have a number of options, based on what you consider an empty String to be, for example...

if (text == null || text.trim().isEmpty()) {...}

Will catch occurrences where the String value is null or is of 0 length when the leading and trailing spaces are removed.

To check if a String contains a valid set of characters, it would be much simpler to use the regular expression functionality supplied by the API, for example...

if (text.matches("[a-z\\-_]+")) {
    System.out.println("Matches");
}

Will match all characters between a and z and the _, - characters. This means that the String must be in lower case ;)

OTHER TIPS

The problem in your code is here:

if(splited[t] != legal[u]){

You cannot use == and != to compare strings. Instead you would have to check:

if(!splited[t].equals(legal[u])){

Furthermore, your logic could be simplified to just one case and an else:

if(splited[t].equals(legal[u])){
    // it is legal
} else {
    // it is not legal 
}

That said, there are much simpler ways to do this. For example, you can access a character directly without the use of split("") by using String.charAt(). You can also directly convert a String to a char[] by using String.toCharArray().

However, you have a general overuse of String (e.g. your String[] could really be a char[]).

You can also quite easily use a regular expression without all these String arrays and splitting, see String.matches(), for example:

if (txt.matches("[a-zA-Z_-]+")) { 
    // string is legal
} else {
    // string is not legal
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top