0

I cant figure out a way to run the following code s.t. it prints out one item in an array every time the enter button is pressed. where i don't know what to write i have written pseudo code.

while(true)
{
    if(enter button pressed)
    {
         print_item();
    }
}

private void print_item()
{
    for(String item: array)
    {
         System.out.println(item);
    }
 }

right now if the code is run it would print all the items when enter is pressed. how would i get the code to print item by item

user3328784
  • 113
  • 7
  • checking the button pressed inside the for loop? – Leo Feb 21 '14 at 23:37
  • there's a subtle difference between checking if a key was pressed and waiting for the user to press a key – Leo Feb 21 '14 at 23:38
  • I hope this helps http://stackoverflow.com/questions/8560395/how-to-use-readline-in-java – Leo Feb 21 '14 at 23:39
  • If you're working in pseudocode, there's no need to worry about trivialities like *implementation details* ;) – Paul Hicks Feb 21 '14 at 23:40

4 Answers4

0
private void print_items()
{
  for(String item: array)
  {
     System.out.println(item);
  }

  waitForEnterButton(); // <===
}

Or possibly

private void print_items() {
  while (moreItems() && waitForEnterButton()) {
    System.out.println(nextItem());
}
Paul Hicks
  • 13,289
  • 5
  • 51
  • 78
0

The reason it is printing all the elements is because when the method print_item() is called, the logic inside it is to print all the elements is item. One way to work around this would be to use flags and pass parameters to your method as follows:

int a = 0;
while(true)
{
    if(enter button pressed)
    {
        print_item(a);
        a = a + 1;
    }
}

private void print_item()
{
    System.out.println(item[a]);
}
ucsunil
  • 7,378
  • 1
  • 27
  • 32
0

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.

Justin
  • 24,288
  • 12
  • 92
  • 142
-1
int index = 0;
while(true)
{
    if(enter button pressed)
    {
         print_item(array[index++]);
    }
}

private void print_item(String item)
{
    System.out.println(item);
}

You would need to add boundary checks to avoid errors.

Max Fichtelmann
  • 3,366
  • 1
  • 22
  • 27