Vlad Demyan Vlad Demyan - 1 month ago 14
Java Question

Iterating in for-each loop Java

I have an ArrayList,where I search for needed element and then add the needed part of it to another ArrayList. The problem is that if I want to keep searching for the words,not only one word,I don't know how to keep going on the elements through the loop. With the using of iterator, I wouldn't be able to search for things I need.

public static ArrayList<String> FindWord(){
ArrayList<String> rewrite=new ArrayList<>();//
ArrayList<String> word=Reading();// rewrites the data from one string to other
String userinput=Chat();
for(String elmt:word){
if (elmt.contains(userinput) && elmt.contains("=")) {
String[] parts=elmt.split("\\=");
rewrite.add(parts[1]);
// here I must do something like word.next
}

}
System.out.println(rewrite);
return rewrite; // RETURNS THE SYNONIM OF THE WORD
}


So,it goes like if I input "hello", it will find me the word "greeting",which is a synonim in my text file. If I input "awesome", it will find the word "thanks", but if I input both of them it will input an empty array, like nothing is found instead of " greeting, thanks"

UPD:
The Reading() returns:

public static ArrayList<String> Reading() {
Scanner inputreader = null;
try {
inputreader = new Scanner(new FileInputStream("D:\\sinonims.txt"));
}catch (FileNotFoundException e1) { // OPENS FILE WITH SINONIMS
e1.printStackTrace();
System.out.println("File not found");
System.exit(0);
}
ArrayList<String> Sins=new ArrayList();
while(inputreader.hasNextLine()){
String l=inputreader.nextLine();
Sins.add(l); // REWRITES DATA FROM FILE TO ARRATLIST
}
inputreader.close();
System.out.print(Sins);
return Sins;
}


public static String Chat(){
System.out.println("Let's start talking.");
Scanner in=new Scanner(System.in);
String line=in.nextLine();
return line;
}

Answer

You should split the input string userinput with some specific delimiter.

Then for each word, Iterate through the splitted array and give each word as input , Find its Synonym with your technique and add it to the arraylist.

This can be implemented by doing some changes in your code.

public static  ArrayList<String> FindWord()
{
    ArrayList<String> rewrite=new ArrayList<>();
    ArrayList<String> word=Reading();
    String userinput=Chat();
    String inputs[]=userinput.split(" "); //Considering delimiter is space(" ") in user input

    for(String input : inputs) //Giving each word as input at a time and search for it in word String
    {
        for(String elmt:word)
        {
            if (elmt.contains(input) && elmt.contains("=")) 
            {
                    String[] parts=elmt.split("\\=");
                    rewrite.add(parts[1]);
            }
        }
    }
    System.out.println(rewrite);
    return rewrite;  // RETURNS THE SYNONIM OF THE WORD
}

So Here I am considering that the input is with delimiter space(" ") so I have splitted input string with space(" ") But If you are taking input with delimiter , then split the input string with it and do the Iteration for each of the separated word.

When you will print the ArrayList, Automatically Output will be printed in separated manner with comma(, ).

So for Input : hello awesome It will give output as greeting, thanks.