简体   繁体   中英

Split a string into an array of strings using substring (Java)

I'm creating some code that prints a message to console with a border around it to reinforce my programming knowledge. I'm having issues with this specific snippet of code that should be splitting a large string into an array of strings which can then be printed

//splits message into multiple parts
//lines is an integer representing how many lines the text would take up within the provided border
//panewidth is an integer representing the desired size of the window created by the borders
    String[] MessageParts = new String[lines];
    for (int i = 0; i < lines; i++){
        MessageParts[i] = (message.substring(i*(panewidth-2), (i+1)*(panewidth - 2)));

        //
        //HACK
        System.out.println(MessageParts[i]);
        //

    }

Full code: ChrisMadeaGame Class:

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package chrismadeagame;

/**
*
* @author 570694
*/
public class ChrisMadeaGame {

/**
 * @param args the command line arguments
 */

//Generates a statholder object for score
    StatHolder Score = new StatHolder();
    StatHolder Turns = new StatHolder();
public static void main(String[] args) {
    // TODO code application logic here
    ChrisMadeaGame ChrisMadeaGame = new ChrisMadeaGame();
    ChrisMadeaGame.display("Test");
}

public void display(String message) {
    //Width of pane goes here
    final int panewidth = 80;
    //The character used for the border
    final String BorderChar = "*";
    //The character used for whitespace
    final String WhitespaceChar = " ";

    //Calculates how many lines will be necessary to print the message. Always rounds up to an integer
    final int lines = (int) Math.ceil((panewidth - 2)/message.length());

    //
    //HACK
    System.out.println(lines);
    System.out.println(message.length());
    System.out.println(panewidth);
    System.out.println((panewidth - 2)/message.length());
    //

    //splits message into multiple parts
    String[] MessageParts = new String[lines];
    for (int i = 0; i < lines; i++){
        MessageParts[i] = (message.substring(i*(panewidth-2), (i+1)*(panewidth - 2)));

        //
        //HACK
        System.out.println(MessageParts[i]);
        //

    }
    //Prints out the top border
    for (int i = 0; i < panewidth; i++){
        System.out.print(BorderChar);
    }
    System.out.println("");
    //Prints the score line
    System.out.print(BorderChar);
    System.out.print("");
    //Figures out how much whitespace there needs to be after printing the score info
    System.out.print("Score: " + Score.get() + " Turns: " + Turns.get());
    for (int i = 0; i < panewidth -17 - Score.length() - Turns.length(); i++){
        System.out.print(WhitespaceChar);
    }
    System.out.print(BorderChar);
    System.out.println("");
    //prints the message
    for (int i = 0; i < lines; i++){
        System.out.print(BorderChar);
        System.out.print(MessageParts[i]);
        System.out.print(BorderChar);
        System.out.println("");
    }

}
}

StatHolder Class:

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package chrismadeagame;

 /**
  *
  * @author 570694
  */
public class StatHolder{
    //Generic object for holding a single integer
    private int stat;
    //Constructor
    public StatHolder(int newStat){
        stat = newStat;
    }   
    public StatHolder(){
        stat = 0;
    }

    //Methods
    public void set(int stat){};
    public int get(){return stat;};
    public int length(){
        return String.valueOf(stat).length();
    }
    };

Stack Trace:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 78
at java.lang.String.substring(String.java:1907)
at chrismadeagame.ChrisMadeaGame.display(ChrisMadeaGame.java:50)
at chrismadeagame.ChrisMadeaGame.main(ChrisMadeaGame.java:25)
Java Result: 1

As you can see there is java.lang.StringIndexOutOfBoundsException: String index out of range: 78 There is no 78th position in your array.

Please check the value for the number of lines because that's what is the size of the array you are defining: String[] MessageParts = new String[lines];

It actually depends on the length of your message:

final int lines = (int) Math.ceil((panewidth - 2)/message.length());

what kind of exception do you get? IndexOutOfBOunds?

If that is the case then the string that you pass as parameter can not be sliced in so many parts as the number "lines"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM