You wrote in your question:
I also would like it to, instead of cutting off at the twentieth character, cut off at the space before that.
Consider the following code. It uses method printf (of class java.io.PrintStream
) to print each line of output as a string of 20 characters with a single leading and single trailing asterisk (*
). The format string is:
*%-20s*%n
which means that the string is left justified and padded with trailing spaces if it is less than 20 characters long and it is followed by a newline. As mentioned, the *
characters are literals.
I suggest that you run it with a debugger as I believe that will help you to understand how it works.
import java.util.Scanner;
public class NewTry {
public static void main(String[] args) {
System.out.println("Please input a word: ");
Scanner stringScanner = new Scanner(System.in);
String input = stringScanner.nextLine();
input = input.trim();
int numberOfCharacters = 20;
String format = "*%-" + numberOfCharacters + "s*%n";
System.out.println("**********************");
int total = input.length();
int start = 0;
String hyphen;
String substr;
while (start + numberOfCharacters < total) {
if (input.charAt(start) == ' ') {
start++;
}
hyphen = "";
int index = start + numberOfCharacters;
if (index < total) {
while (index > start && input.charAt(index) != ' ') {
index--;
}
if (index == start) {
index = start + numberOfCharacters - 1;
hyphen = "-";
}
}
substr = input.substring(start, index);
if (substr.charAt(0) == ' ') {
substr = substr.substring(1);
index++;
start++;
int temp = start + numberOfCharacters;
if (temp < total) {
if (input.charAt(temp) == ' ') {
index = temp;
substr = input.substring(start, index);
}
}
}
System.out.printf(format, substr + hyphen);
start = index;
}
if (start < total) {
substr = input.substring(start);
if (substr.charAt(0) == ' ') {
substr = substr.substring(1);
}
System.out.printf(format, substr);
}
System.out.println("**********************");
stringScanner.close();
}
}
The above code assumes the following regarding the string entered by the user:
- The string contains no punctuation, i.e. commas, hyphens, etc.
- Words in the string are separated by a single space only, i.e. no TAB characters or more than one consecutive spaces.
Note that the above code also handles words that are longer than 20 characters. It does so by taking 19 characters and adding a hyphen.
Here are my test runs:
Please input a word:
George Best was a professional footballer who began his professional career playing for English football club Manchester United at the age of 17
**********************
*George Best was a *
*professional *
*footballer who began*
*his professional *
*career playing for *
*English football *
*club Manchester *
*United at the age of*
*17 *
**********************
Please input a word:
antidisestablishmentarianism
**********************
*antidisestablishmen-*
*tarianism *
**********************
Please input a word:
can you spell antidisestablishmentarianism
**********************
*can you spell *
*antidisestablishmen-*
*tarianism *
**********************
Please input a word:
antidisestablishmentarianism is a long word
**********************
*antidisestablishmen-*
*tarianism is a long *
*word *
**********************
Please input a word:
please find the word antidisestablishmentarianism in the dictionary
**********************
*please find the word*
*antidisestablishmen-*
*tarianism in the *
*dictionary *
**********************
Please input a word:
stackoverflow
**********************
*stackoverflow *
**********************