Array Input Loop
The below command line program demonstrates use of an array to handle an arbitrary number of input variables.
While loops are used for programmers still unfamiliar with for loop syntax.
Main.java
package ceccs;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
String[] sentences;
Scanner scan = new Scanner(System.in);
System.out.println("Input as many sentences as you'd like. How many sentences?");
int numSen = scan.nextInt();
//initialize array to have requested number of variables
sentences = new String[numSen];
// take all input entries in a loop
scan = new Scanner(System.in); // clear System.in buffer - it may have an extra line return.
int i = 0;
while (i < numSen){
System.out.println("enter sentence number " + i);
sentences[i] = scan.nextLine();
i++;
}
System.out.println("Here are all of the input sentences:");
System.out.println("------------------------------------");
// a second loop to print all values in the input array
i = 0;
while (i < numSen){
System.out.println(i + ": " + sentences[i]);
i++;
}
}
}