This code takes an input string from the user, which may contain special characters. We then output the letters that are missing from the alphabet. For example, "the quick brown fox jumped over the lazy dog" would have an empty string returned, but "ZYXW, vu TSR Ponm lkj ihgfd CBA." would be missing the letters "eq".
Currently, my program is returning the whole alphabet instead of only the missing characters.
import java.io.*;
import org.apache.commons.lang3.*;
public class QuickBrownFox {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine();
s = s.toUpperCase();
String[] arr = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S",
"T", "U", "V", "W", "X", "Y", "Z" };
String chars_only = "";
for (int i = 0; i < s.length(); i++) {
for (int j = 0; j < arr.length; j++) {
if (s.substring(i, i + 1).equals(arr[j])) {
chars_only += s.substring(i, i + 1);
}
}
}
System.out.println(chars_only); // now we have a string of only alphabet letters
String missing = "";
for (int j = 0; j < arr.length; j++) {
if (StringUtils.contains(arr[j], chars_only) == false) { // alphabet letter not found
missing += arr[j];
}
}
missing = missing.toLowerCase();
System.out.println("missing letters: " + missing);
}
}
StringUtils.contains(arr[j], chars_only)checks whether the alphabet letter (arr[j]) contains the whole stringchars_onlywhich will never be the case (unless you enter just one character). What you want is!chars_only.contains(arr[j])(no need to useStringUtils.contains). This will check whether the inputted letters do not contain thejth alphabet letter. Alternatively you can keepStringUtils.contains, just swap the parameters:!StringUtils.contains(chars_only, arr[j]).Btw, you could use streams to reduce the amount of code: