0

I looked around and haven't found anything that I really understand or can do to do this and am hoping there is a simple solution. I have to add a * in the 20th character slot after each line in this program.

This is my code:

package StarBorder;

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();
        int numberOfCharacters = 20;
        System.out.println("**********************");
        for (int i = 0; i < input.length(); i += numberOfCharacters) {
            String part = input.substring(i, Math.min(input.length(), i + numberOfCharacters));
            System.out.println("*" + part + "*");
        }
        System.out.println("**********************");
    }
}

This is what it prints out:

**********************
*Hello I am doing som*
*e code, hello ther p*
*eople on stack overf*
*low* // I need this to be over where the other *'s are.
**********************

I need it to be a full square of stars. Thanks for any answers.

I also would like it to, instead of cutting off at the twentieth character, cut off at the space before that. That is a secondary issue, however.

Abra
  • 19,142
  • 7
  • 29
  • 41
  • you're looking for "how to pad a string in java" for the case you're flagging. If `*` needs to be at the 20th spot, and you have a string that's only four letters long, you're going to need (20 - 5) spaces before writing that `*`. Thankfully, there are several easily found answers on SO for how to pad strings in Java. – Mike 'Pomax' Kamermans Mar 08 '23 at 04:27
  • Just search for space padding or string padding. For e.g. https://stackoverflow.com/questions/388461/how-can-i-pad-a-string-in-java – user3437460 Mar 08 '23 at 04:54

4 Answers4

1

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       *
**********************
Abra
  • 19,142
  • 7
  • 29
  • 41
0

A simple way to do this would be to simply to print out a bunch of spaces to fill in the empty space (padding the string). For example, for the last line in input you could do

    System.out.print("*" + part);
    for(int i=0; i < numberOfCharacters - 2 - part.length(); i++){
        System.out.print(" ");
    }
    System.out.println("*");

What this code is doing is just finding the difference between the part's length, and the length of an ideal substring, then prints out that many spaces.

0
System.out.printf("*%-20s*%n", part);

This will print part formatted, left aligned (-), paddel to 20. %n for a line break.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
0
public static void main(String[] args) {

        System.out.println("Please input a word: ");
        Scanner stringScanner = new Scanner(System.in);
        String input = stringScanner.nextLine();
        int numberOfCharacters = 20;
        System.out.println("**********************");
        for (int i = 0; i < input.length(); i += numberOfCharacters) {
            String part = input.substring(i, Math.min(input.length(), i + numberOfCharacters));
            System.out.print("*" + part);
            // Quoting the comment area answer, there should not be a minus 2 here
            for (int j = 0; j < numberOfCharacters - part.length(); j++) {
                System.out.print(" ");
            }
            System.out.println("*");
        }
        System.out.println("**********************");
    }