使用substring(Java)将字符串拆分为字符串数组

时间:2014-01-28 00:09:17

标签: java arrays string

我正在创建一些代码,将一条消息打印到控制台,并在其周围加上边框,以加强我的编程知识。我遇到了这个特定代码片段的问题,这些代码片段应该将一个大字符串拆分成一个字符串数组,然后可以打印出来

//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]);
        //

    }

完整代码: ChrisMadeaGame课程:

/*
* 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类:

/*
 * 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();
    }
    };

堆栈追踪:

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

2 个答案:

答案 0 :(得分:1)

正如您所看到的那样java.lang.StringIndexOutOfBoundsException: String index out of range: 78阵列中没有第78个位置。

请检查行数的值,因为这是您定义的数组的大小: String[] MessageParts = new String[lines];

实际上取决于邮件的长度:

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

答案 1 :(得分:0)

你得到了什么样的例外? IndexOutOfBOunds?

如果是这种情况,那么作为参数传递的字符串不能像数字“lines”那么多部分切片