I want the each line to split off after 20 characters have gone by but I want it to split off at the nearest space so the sentence has only whole words.
This is the code that I have:
//I have imported the scanner util above this.
System.out.println("Please input a word: ");
Scanner stringScanner = new Scanner(System.in);
String input = stringScanner.nextLine();
int numberOfCharacters = 20;
for (int i = 0; i < input.length(); i += numberOfCharacters) {
String part = input.substring(i, Math.min(input.length(), i + numberOfCharacters));
System.out.println(part);
}
}
}
This is the output I get:
Please input a sentence:
hello there, I am doing some coding!
hello there, I am do
ing some coding!
This is what I want:
Please input a sentence:
hello there, I am doing some coding!
hello there, I am
doing some coding!
I am not sure how to get this to work. Thank you for any anwers.