How do you print x number of lines at a time

I have a program that prints instance variables of objects stored in an arraylist. I want to add a function to this program so that it only prints the instance variables for the first 10 objects and then prompts the user to press Enter to continue, then continue with the next 10 objects, etc.

Here's an example of what the print loop looks like without the function I'm asking for:

  for (int i = 0; i < myList.size(); i++)
  {
    System.out.println(myList.get(i).getName();
  }

      

Details of "Click to Continue" are simple enough:

import java.util.*;
...
Scanner input = new Scanner(System.in);
input.nextLine()

      

but how to create a conditional loop that stops after 10 prints and then resumes after the "Press enter to continue" event

+3


source to share


5 answers


I decided to go my own way. I wish I could do justice to each type of answer, since my suggestion is a combination of all:

for (int i = 0; i < myList.size(); i++)
{
  if(i % 10 == 0 && i != 0){
    System.out.println("Press enter to continue...");
    input.nextLine();
  }
  System.out.println(myList.get(i).getName());
}

      



Using printing outside of an if statement makes sense. It now prints 10 statements correctly the first time instead of 11.

0


source


for (int i = 0; i < myList.size(); i++)
  {
    System.out.println(myList.get(i).getName();
    if(i % 10 == 0){
         input.nextLine()
    }
  }

      



+3


source


You can use System.in.read()

, it reads a character and hence when the user presses any key it continues.

Other wise ones are waiting for the user to enter.

for (int i = 0; i < myList.size(); i++)
  {
    System.out.println(myList.get(i).getName();
    if(i % 10 == 0){
         System.out.println("Press any key to continue!");
         char ch = (char) System.in.read();
    }
  }

      

+2


source


Try this code:

for(int index = 0; i <myList.size(); i++){

         /*check if it has printed 10 times first or else, it'll be printed 11 times first*/
         if( i % 10 == 0 && i!=0 ){
             System.out.println("Press enter to continue...");
             //Prompt the user of course... 
             input.nextLine();
         }
         System.out.println(myList.get(i).getName());
}

      

as mentioned in the above code, you need to check if it is printed 10 times in the first place. @Bmscomp code may have an error if it contains more than 10 entries.

+1


source


I believe you can get a better understanding of what to do about receiving user input. Please try this: - `

import java.util.Scanner;

public class GetInputFromUser {

public static void main(String args[]) {
    int a;
    float b;
    String s;

    Scanner in = new Scanner(System.in);

    System.out.println("Enter a string");
    s = in.nextLine();
    System.out.println("You entered string " + s);

    System.out.println("Enter an integer");
    a = in.nextInt();
    System.out.println("You entered integer " + a);

    System.out.println("Enter a float");
    b = in.nextFloat();
    System.out.println("You entered float " + b);
}
}

      

-1


source







All Articles