Check if the string contains only a set of letters

I am trying to check if a word contains only a set of letters, such as I, O, S, H and X. Suppose the user enters: SSHX, the output will be yes, but if the user enters SHEXX, the output will be NO

public static void main(String[] args) { Scanner sc = new Scanner(System.in); String word = sc.next(); word = word.toUpperCase(); int length = word.length(); char letter = 0; for (int counter = 0; counter < length; counter++) { letter = word.charAt(counter); } if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') { System.out.print("NO"); } else { System.out.print("YES"); } } 
+8
java string char
source share
5 answers

You have a good way to solve this. The problem is that you do not actually check every letter, so you need to do checks inside the for loop or you will only check the last letter. But then you cannot print β€œYES”, because you want to print it only if all the letters are β€œyes”, so you can use a boolean to check this as such:

  boolean isMatch = true; for (int counter = 0; counter < strLength && isMatch; counter++) { letter = word.charAt(counter); if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') { System.out.print("NO"); isMatch = false; } } if (isMatch) { System.out.print("YES"); } 

But, as others have pointed out, using a regex is more efficient (and it has a working regex for whatever you want. An asterisk means zero or more of what is inside the brackets.):

  if (word.matches("[HIOSX]*")) { System.out.print("YES"); } else { System.out.print("NO"); } 
+11
source share

Beyond the obvious answer to using regex, consider using the Google Guava API to make this pretty simple:

 if(CharMatcher.anyOf("HIOSX").matchesAllOf(word)) { } ... 
+5
source share

Use regex .

 String regex = "[OSXHI]*"; String string = "SOMETHING"; Matcher matcher = Pattern.compile(regex).matcher(string); if (matcher.find()) { String match = matcher.group(1); System.out.println(match); } 

Some additional resources:

+4
source share

Use regex:

 if (word.matches("[HIOSX]+")) System.out.println("YES"); else System.out.println("NO"); 
+2
source share

First of all, you must initialize the letter as follows: char letter = '0'; instead of 0, secondly, your for loop is poorly used. Try this code:

  boolean isInSet; for (int counter = 0; counter < strLength; counter++) { letter = word.charAt(counter); if (letter != 'I' && letter != 'O' && letter != 'S' && letter != 'H' && letter != 'Z' && letter != 'X' && letter != 'N') { isInSet=false; counter=strlength; //end loop } else { isInSet=true; } } if(isInSet=true) { System.out.print("YES"); } else { System.out.print("NO"); } 

Now the loop will iterate over the string and check whether each character is in the set, if it does not end with an end, and the logical value is false, which leads to the output of NO

0
source share

All Articles