I have to build a program Vocabulary with ArrayList
. The words are added in this ArrayList
. Then I have to check whether the word inputted:
- has more than two
- is only one word
- does not contain certain characters.
At the end I have to check words in list with the three first characters of the string inputted and return the words found. Here is my code:
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;
}
}