Let's address your problems one at a time. First up, why all the items are printed at once.
If you look closely at your code, it is clear why all the items are printed at once.
if(enter button pressed)
{
print_item();
}
There is no error there. Let's look at the method itself:
private void print_item()
{
for(String item: array) //Wait, for every string in the array!
{
System.out.println(item);
}
}
The for(String item: array)
runs the printline for every element in your array.
Instead, have your print_item
method accept an integer. Something like this:
private void print_item(int index) {
System.out.println(array[index]); //actually, just call this line instead of the method
}
And change your loop:
for (int index = 0; index < array.length; index++) {
wait_for_enter_to_be_pressed;
print_item(index); //Should be System.out.println(array[index]);
}
For waiting, it depends on what type of application you are using. For a console application, this is one option:
Scanner waitForEnter = new Scanner(System.in);
for (int index = 0; index < array.length; index++) {
waitForEnter.nextLine();
System.out.println(array[index]);
}
waitForEnter.nextLine();
is how the Scanner
accepts input, so this will wait until input is given, signified by the enter.