next up previous
Next: Loop scheme for a Up: Unit 06 Previous: Loop schemes

Loop scheme for a counter: number of strings in input

Count the number of times that a user inputs a string.

String s;       // current string in input
int counter;

counter = 0;
s = JOptionPane.showInputDialog("Input a string");
while (s != null) {
  counter++;
  s = JOptionPane.showInputDialog("Input a string");
}

System.out.println("Number of strings in input = " + counter);


next up previous
Next: Loop scheme for a Up: Unit 06 Previous: Loop schemes