I'm working on a program that asks user for values, and adds them onto array list until value of -1 is inserted. Then it asks, what value do you want to search for and user inserts another value. Then the program searches for all of those values in the array list, and prints out the indexes of all the found items. My program currently prints only the first item and it's index since i'm using list.indexOf(searcheditem). How do i get it to list all of the found items and not only the 1st one? Heres my code so far.
import java.util.ArrayList;
import java.util.Scanner;
public class KysytynLuvunIndeksi {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
ArrayList<Integer> list = new ArrayList<>();
while (true) {
int read = Integer.parseInt(reader.nextLine());
if (read == -1) {
break;
}
list.add(read);
}
System.out.print("What are you looking for? ");
int searched = Integer.parseInt(reader.nextLine());
if (list.contains(searched)) {
System.out.println("Value " + searched + " has index of: " + (list.indexOf(searched));
} else {
System.out.println("value " + searched + " is not found");
}
}
}
Thanks in advance!