此代码从用户那里获取输入字符串,其中可能包含特殊字符。然后我们输出字母表中缺少的字母。例如,“the quick brown fox jumped over the lazy dog”会返回一个空字符串,但是“ZYXW, vu TSR Ponm lkj ihgfd CBA”。会丢失字母“eq”。
目前,我的程序正在返回整个字母表,而不仅仅是丢失的字符。
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);
}
}
答案 0 :(得分:0)
StringUtils.contains(arr[j], chars_only)
检查字母 (arr[j]
) 是否包含整个字符串 chars_only
,这永远不会是这种情况(除非您只输入一个字符)。您想要的是 !chars_only.contains(arr[j])
(无需使用 StringUtils.contains
)。这将检查输入的字母是否不包含第 j
个字母。或者,您可以保留 StringUtils.contains
,只需交换参数:!StringUtils.contains(chars_only, arr[j])
。
顺便说一句,您可以使用流来减少代码量:
import java.util.Arrays;
import java.util.List;
import java.io.*;
import java.util.stream.Collectors;
public class QuickBrownFox {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String s = reader.readLine().toUpperCase();
List<String> alphabet = Arrays.asList("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 missing = alphabet.stream()
.filter(letter -> !s.contains(letter))
.collect(Collectors.joining())
.toLowerCase();
System.out.println("missing letters: " + missing);
}
}
答案 1 :(得分:0)
一种选择是使用用给定的 Set
初始化的 alphabet-chars
。然后从 set
中删除包含在 chars
中的任何 input-string
。剩余部分将反映缺失的 chars
。
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
public class TestAlphabet {
public static void main(String[] args) {
Set<Character> set = new HashSet<Character>();
Character[] 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' };
Collections.addAll(set, arr);
String s1="abc";
System.out.println("input:"+s1);
new TestAlphabet().new Missing().getMissing(set, s1).forEach(System.out::print);
System.out.println("\n");
String s2="the quick brown fox jumped over the lazy dog";
System.out.println("input:"+s2);
new TestAlphabet().new Missing().getMissing(set, s2).forEach(System.out::print);
System.out.println("\n");
String s3="ZYXW, vu TSR Ponm lkj ihgfd CBA.";
System.out.println("input:"+s3);
new TestAlphabet().new Missing().getMissing(set, s3).forEach(System.out::print);
}
class Missing
{
public Set<Character> getMissing(Set<Character> orig, String s)
{
//keep origin
Set<Character> set = new HashSet<Character>();
set.addAll(orig);
s=s.toUpperCase();
for(int i=0;i<s.length();i++)
{
Character c=s.charAt(i);
set.remove(c);
}
return set;
}
}
}
输出
input:abc
DEFGHIJKLMNOPQRSTUVWXYZ
input:the quick brown fox jumped over the lazy dog
S
input:ZYXW, vu TSR Ponm lkj ihgfd CBA.
EQ