Search code examples
javastringsubstringis-empty

Java: Need to print empty spaces in the string using .substring(0,42) method


I am a beginner in programming, but I found it easy until my instructor wanted us to create a static space (billboard) and insert a user-defined string into that space. I figured out how to accept all of the user's input as one string. I even figured out how to cut into parts that string to print within the defined space. However, if the user input is too short, I get an OutOfBoundsException. If the string is too long, it prints to all the available, user-defined space; then it prints the entire string, again, below the defined space.

I need to know how to accept the empty spaces. I also need to know how to place a limit on the characters printed.

Here is the code:

//this is a subclass of the Billboard superclass
package itsd322_u4_ip;
import java.util.Scanner;
public class Create extends Billboard {
Scanner scan = new Scanner(System.in);
String userInput = "";
{
    System.out.println("Enter your message [0-336 caharacters]:\n");
    userInput = scan.nextLine();


    System.out.println("**********************************************");
    System.out.println("*                                            *");
    System.out.println("* Your message was:                          *");
    System.out.println("*                                            *");
    System.out.println("* " + userInput.substring(0,42) +          " *");
    System.out.println("* " + userInput.substring(42,84) +         " *");
    System.out.println("* " + userInput.substring(84,126) +        " *");
    System.out.println("* " + userInput.substring(126,168) +       " *");
    System.out.println("* " + userInput.substring(168,210) +       " *");
    System.out.println("* " + userInput.substring(210,252) +       " *");
    System.out.println("* " + userInput.substring(252,294) +       " *");
    System.out.println("* " + userInput.substring(294,336) +       " *");
    System.out.println("*                                            *");
    System.out.println("**********************************************");
}
@Override
public void displayInfo()
{
    System.out.println(userInput);
    System.out.println();
}
}

Solution

  • for (int i = 0; i < userInput.length(); i += 42) {
        System.out.printf("* %-42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
    }
    

    @ScottRLemon's suggestion ensures that the starting index for substring is going to be valid all the time. However you also have to do the same for the ending index (that is what the Math.min call does here), and for good-looking purposes you have to make sure that the given line has 42 characters even if a shorter portion is printed (can -and usually does- happen at the end of the string). Format string %s is for string, %42s is for 42 characters and %-42s is for 42 characters where shorter string is aligned to the left.
    printf prints, which is okay here. However if you need to store the result, String.format could do that:

    String somestring=String.format("%-42s", "Hello");
    


    One way to maintain board-size:

    int i=0;
    while(i < userInput.length()){
        System.out.printf("* %42s *\n", userInput.substring(i, Math.min(i + 42, userInput.length())));
        i += 42;
    }
    while(i < 336){
        System.out.println("*                                            *");
        i+=42;
    }