Java arraylist 词汇程序
Java vocabulary program with arraylist
我必须用 ArrayList
构建一个程序 Vocabulary。话加在这ArrayList
。然后我必须检查输入的单词是否:
- 有两个以上
- 只有一个字
- 不包含某些字符。
最后我必须用输入的字符串的前三个字符和 return 找到的单词来检查列表中的单词。这是我的代码:
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;
}
}
如果我没有正确理解你的问题,你正在寻找这样的东西:
if (isValidInput(input)) {
String first3 = input.substring(0, 3);
for (String word : vocabularyList) {
if (word.startsWith(first3)) {
System.out.println(word);
}
}
}
我必须用 ArrayList
构建一个程序 Vocabulary。话加在这ArrayList
。然后我必须检查输入的单词是否:
- 有两个以上
- 只有一个字
- 不包含某些字符。
最后我必须用输入的字符串的前三个字符和 return 找到的单词来检查列表中的单词。这是我的代码:
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;
}
}
如果我没有正确理解你的问题,你正在寻找这样的东西:
if (isValidInput(input)) {
String first3 = input.substring(0, 3);
for (String word : vocabularyList) {
if (word.startsWith(first3)) {
System.out.println(word);
}
}
}