简体   繁体   English

数组越界? 带字符串分割

[英]Array out of Bounds? with string split

Array out of bounds ? 数组越界? i'm trying to perform the output in the picture: 我正在尝试执行图片中的输出:

在此处输入图片说明

Using this INPUT "JAVA IS A PROGRAMMING LANGUAGE" 使用此输入“ JAVA是一种编程语言”

This is my code so far 到目前为止,这是我的代码

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

When the second word reaches its last letter it doesn't continue the for loop, is there any way to continue the loop even if the second word reaches its max length. 当第二个单词到达最后一个字母时,它不会继续for循环,即使第二个单词达到其最大长度,也有任何方法可以继续循环。

< should have been <= . <应该是<= Reversing left and right hand sides makes it more readably I think. 我认为,反转左右两侧使其更加可读。

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

Or instead of the if-else statement: 或代替if-else语句:

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

This gives the result you want: 这给出了您想要的结果:

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

This line was changed: 此行已更改:
if(word[b].length() < a + 1) and not if(word[b].length() < a) if(word[b].length() < a + 1)而不是if(word[b].length() < a)
and 2 spaces print in the if statement 和2个空格打印在if语句中

TRY THIS SOLUTION HOPE IT WILL HELP YOU : 尝试此解决方案可帮助您:

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("");

        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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