我必须使用ArrayList
构建一个程序词汇表。这些字词会添加到ArrayList
中。然后我要检查输入的单词是否是
最后,我必须检查列表中的单词,输入字符串的前三个字符并返回找到的单词。这是我的代码:
import java.util.ArrayList;
import java.util.Scanner;
public class Vocabulary {
public static void main(String[] args) {
ArrayList<String> vocabularyList=new ArrayList<String>();
vocabularyList.add("vocabulary");
vocabularyList.add("article");
vocabularyList.add("java");
vocabularyList.add("program");
vocabularyList.add("calendar");
vocabularyList.add("clock");
vocabularyList.add("book");
vocabularyList.add("bookshop");
vocabularyList.add("word");
vocabularyList.add("wordpress");
Scanner input=new Scanner(System.in);
System.out.println("Enter the word: ");
String wordInputed=input.nextLine();
input.close();
}
private static boolean isValidInput(String wordInputed){
boolean result=true;
if (wordInputed.trim().length()<2){
System.out.println("Please enter a full word");
result=false;
}
else if(wordInputed.trim().indexOf(" ")>-1){
System.out.println("Please enter only one word");
result=false;
}
else if(wordInputed.trim().toLowerCase().contains("%") || wordInputed.trim().toLowerCase().contains("@") || wordInputed.trim().toLowerCase().contains("&") ){
System.out.println("Please enter an word that doesnt contains character: %, & and @");
result=false;
}
return result;
}
}
答案 0 :(得分:0)
如果我理解你的问题,你就会找到这样的东西:
if (isValidInput(input)) {
String first3 = input.substring(0, 3);
for (String word : vocabularyList) {
if (word.startsWith(first3)) {
System.out.println(word);
}
}
}