next up previous
Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for an

Loop scheme for an accumulator: concatenation of strings

Concatenate the strings in input that start with a ':'.

String s;         // current input string
String stot;      // variable used as accumulator

stot = "";        // "" is the neutral element for concatenation
s = JOptionPane.showInputDialog("Input a string");
while (s != null) {
  if (s.charAt(0) == ':')
    stot = stot + s;
  s = JOptionPane.showInputDialog("Input a string");
}

System.out.println("total string = " + stot);


next up previous
Next: Loop scheme for characteristic Up: Unit 06 Previous: Loop scheme for an