I want to build a simple dictionary search program without using dictionary library. I want to search the string array if the string in array is equal to the char array. It should print the string and its index number. If there are 2 strings that are matching the char then it should print the first string and leave the after string
.e.g String array["fine","rest","door","shine"] char character ['t','r','e','s']. the answer should be "rest" at index 1. and if the String rest is repeat then it should only print first one.
I tried to compare the string array and char array but its returning all the words of string array that matches char array.
String strArray[]=new String[4];
char chrArray[]=new char[4];
String value="";
char compare;
System.out.println("Enter the words :");
for(int i=0;i<strArray.length;i++){
strArray[i]=input.next();
}
System.out.println("Enter the Characters :");
for (int i = 0; i < chrArray.length; i++) {
chrArray[i]=input.next().charAt(0);
}
for (int i = 0; i < strArray.length; i++) {
if(strArray[i].length()==chrArray.length){
if(""+strArray[i]!=value){
value="";
}
for (int j = 0; j < strArray[i].length(); j++) {
for (int k = 0; k < chrArray.length; k++) {
if(strArray[i].charAt(j)==chrArray[k]){
value=value+strArray[i].charAt(j);
}
}
}
}
}
System.out.println(value);
The output should be the string from array that is equal to char array.
if(""+strArray[i]!=value){" isn't the correct way to compare strings. The+is redundant (unless you expectstrArray[i]to be null), and then you'd need to useequals(eitherstrArray[i].equals(value), orObjects.equals(strArray[i], value)if you think it might be null).