数组越界?带字符串分割

时间:2018-10-18 12:59:15

标签: java arrays

数组超出范围?我正在尝试执行图片中的输出:

enter image description here

使用此输入 “ JAVA是一种编程语言”

到目前为止,这是我的代码

import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Input Phrase:");
        String s = in.nextLine();

        String[] word=s.split(" ");
        String rts=" ";

        for(int i=0;i<word.length;i++){
            if(word[i].length()>=rts.length()){
                rts=word[i];
            }
        }

        int thisislength = rts.length();

        for (int a = 0; a < thisislength ;a++ ) {
            for (int b = 0; b < word.length ;b++ ) {
                System.out.print(word[b].charAt(a)+" ");  
            }
            System.out.println();
        }
    }
}

当第二个单词到达其最后一个字母时,它不会继续for循环,即使第二个单词达到其最大长度,也有任何方法可以继续循环。

3 个答案:

答案 0 :(得分:1)

<应该是<=。我认为反转左右手可以使其更加可读。

    for (int a = 0; a < thisislength; a++) {
        System.out.printf("%3d ", a+1);
        for (int b = 0; b < word.length; b++) {
            if (a >= word[b].length()) {
                System.out.print(' ');
            } else {
                System.out.print(word[b].charAt(a));  
            }
            System.out.print(' ');
        }
        System.out.println();
    }

或者代替if-else语句:

        for (String w : word) {
            System.out.print(a >= w.length() ? ' ' : w.charAt(a));  
        }

答案 1 :(得分:1)

这将提供您想要的结果:

for (int a = 0; a < thisislength ;a++ ){
    for (int b = 0; b < word.length ;b++ ){
        if(word[b].length() < a + 1){
            System.out.print("  ");
        }else{
            System.out.print(word[b].charAt(a) + " ");
        }
    }
    System.out.println();
}

此行已更改:
if(word[b].length() < a + 1)而不是if(word[b].length() < a)
if语句中将显示2个空格

答案 2 :(得分:0)

尝试此解决方案将帮助您:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main
{
    public static void main(String[] args) {

        // GET VALUE FROM THE CONSOLE
        Scanner in = new Scanner(System.in);
        System.out.print("Input Phrase:");
        String s = in.nextLine();

        // SPLIT STRING TO WORDS
        String[] words = s.split(" ");

        // CREATE A LIST OF CHAR_ARRAY CALLED : matrix
        List<char[]> matrix = new ArrayList<char[]>();

        // REFERENCE THE LARGEST WORD IN WORDS ARRAY EX : PROGRAMMING IS THE LARGEST 
        int max = 0;

        // FILL OUR LIST OF ARRAY OF CHARS
        for (int b = 0; b < words.length ;b++ ) {
            char[] chars =  words[b].toCharArray();
            max = (chars.length >= max)? chars.length :  max ;
            matrix.add( chars );
        }

        // PRINT OUR CHAR
        for (int a = 0; a < max ;a++ ) {

            for (int b = 0; b < words.length ;b++ ) {

                if(a < matrix.get(b).length) {
                    System.out.print(matrix.get(b)[a]);
                    System.out.print(" ");
                }else {
                    System.out.print(" ");
                    System.out.print(" ");
                }

            }

            System.out.println("");

        }

    }
}