在下面的代码中,我试图从用户那里获取一个输入,如果用户输入了一个像?AEIRST的输入?由A-Z中的字母替换,然后从文件中检查字谜。但程序崩溃了。请帮忙。
以下是程序崩溃的部分的代码片段:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
anagram=(EditText)findViewById(R.id.inputtext);
submit=(Button)findViewById(R.id.submit);
viewall=(TextView)findViewById(R.id.list);
viewall.setMovementMethod(new ScrollingMovementMethod());
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String a =anagram.getText().toString();
checkword("cswfifteen.txt",a);
}
});
}
private void checkword(String Filename, String a) {
StringBuilder builder = null;
BufferedReader reader = null;
ArrayList<String> words = new ArrayList<>();
try {
builder = new StringBuilder();
reader = new BufferedReader(new InputStreamReader(
getAssets().open(Filename)
));
String Line;
while ((Line = reader.readLine()) != null) {
builder.append(Line).append("\n");
}
String[] dict = builder.toString().split("\n");
if (a.matches("[?]+")) {
for (String i : dict) {
char[] b = i.toString().toCharArray();
Arrays.sort(b);
char[] c = a.toCharArray();
Arrays.sort(c);
if (b.length == a.length()) {
words.add(i);
}
}
} else if (a.indexOf("?")!=-1) {
for (String i : dict) {
for (char j : alphabets) {
a = a.replace('?', j);
char[] c=a.toUpperCase().toCharArray();
Arrays.sort(c);
viewall.setText(String.valueOf(c));
char[] b = i.toString().toCharArray();
Arrays.sort(b);
if (String.valueOf(b).contains(String.valueOf(c)) && b.length==c.length) {
words.add(i);
}
}
}
} else {
for (String i : dict) {
char[] b = i.toString().toCharArray();
Arrays.sort(b);
char[] c = a.toUpperCase().toCharArray();
Arrays.sort(c);
if (String.valueOf(b).contains(String.valueOf(c)) && b.length==c.length) {
words.add(i);
}
}
}
Collections.sort(words);
StringBuilder result = new StringBuilder();
for (String i : words)
{
result.append(i + "\n");
}
viewall.setText(result.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}