The Answer by YourHelper seems correct.
Avoid char
Unfortunately, even the fixed version of your code will fail with most characters. The char type is essentially broken since Java 2, and legacy since Java 5.
To see char break, use a character like “”.
Code point
Use code point integers instead.
StringBuilder as result
Something like this untested code.
int limit = 5 ;
StringBuilder sb = new StringBuilder() ;
Scanner sc = new Scanner(System.in);
String input;
int codePoint;
for ( int i = 0 ; i < limit ; i++ ) {
input = sc.next();
int[] codePoints = input.codePoints().toArray() ;
if ( codePoints.length == 1 ) {
codePoint = codePoints[ 0 ] ;
System.out.println( "for position " + i + " the input character is: " + Character.toString( codePoint ) );
sb.appendCodePoint( codePoint );
System.out.println("so we get the current result " + sb.toString() );
} else {
System.out.println( "Done: " + sb.toString() ) ;
break;
}
}
System.out.println( "Done: " + sb.toString() ) ;
See this code run live at IdeOne.com.
for index 0 the input character is: H
so we get the current result H
for index 1 the input character is:
so we get the current result H
for index 2 the input character is: l
so we get the current result Hl
for index 3 the input character is: l
so we get the current result Hll
for index 4 the input character is: o
so we get the current result Hllo
Done: Hllo
List of strings (characters) as result
If you want to collect each character individually, make a list of strings. Make a string of each code point, and add to list. Omit the StringBuilder.
List< String > characters = new ArrayList <>() ;
…
characters.add( Character.toString( codePoint ) ) ;
See an example of this code run live at IdeOne.com.
for index 0 the input character is: H
so we get the current result [H]
for index 1 the input character is:
so we get the current result [H, ]
for index 2 the input character is: l
so we get the current result [H, , l]
for index 3 the input character is: l
so we get the current result [H, , l, l]
for index 4 the input character is: o
so we get the current result [H, , l, l, o]
Done: [H, , l, l, o]
Be aware that in some languages what you may perceive as one "letter" is actually a combination of two characters, a letter followed by a "combining diacritical". Example here. Discussion here.