I'm trying to print the first char in one line, then the first two chars in the next line etc. of a string.
I do not understand the reason for a blank line in the beginning and why the last line did not complete the word. I was able to get the desired output by a change in for loop to (i=1 and i<=k).
public class Tester6 {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String userInput = input.nextLine();
int k = userInput.length();
for(int i = 0; i < k; i++) {
System.out.println(userInput.substring(0,i));
}
}
}
The output for the input four is,
f
fo
fou