简体   繁体   English

Java-用户字符串输入转换为6x6的2D数组

[英]Java - User String Input Convert to 2D Array of 6x6

I have a question regarding Java code implementation for the following scenario, 对于以下情况,我对Java代码实现有疑问,

  1. Get User Input [String from Scanner] 获取用户输入[来自扫描仪的字符串]
  2. Convert the String input to 1D Array 将字符串输入转换为一维数组
  3. From the 1D char array, populate it accordingly to 2D char array based on positions, [0][0], [0][1] etc 从1D字符数组,根据位置[0] [0],[0] [1]等将其相应地填充到2D字符数组

What I have done is as follows, 我所做的如下

charArray has been created as a 6 by 6 array. charArray已创建为6 x 6数组。 char[][] charArray = new char[6][6];

Scanner scn = new Scanner (System.in);
System.out.print("String text: "); //Text from user
String str= scn.nextLine();
str = str.replaceAll("\\s+","");
char[] cArray = text.toCharArray(); //Convert String to char array

for (int i = 0; i < cArray.length; i++)
{
        for (int row = 0; row < charArray.length; row++)
        {

            for (int col = 0; col < charArray[row].length; col++)
             {

                 System.out.println("i = " + i);
                 //charArray[row][col] = letters[i]; //I'm supposed to put in the converted String to char and put into the 2D Array of 6 by 6. When I executed the script, there is an array out of bound exception 
                 System.out.println("charArray[" + row + "]" + "[" + col + "]" + "cArray[" + i + "]");
                 ++i;
                 System.out.println("End of col for loop");
              }
                   System.out.println("End of row for loop");

         }           
              i = i;

    }    
    System.out.println("End of i for loop");

It seems from the printing of messages, I could get the text's i position and to it's corresponding 2D array positions. 从消息的打印看来,我可以获得文本的i位置及其对应的2D数组位置。 But I could not populate the values by assigning. 但是我无法通过分配来填充值。

I appreciate any tips and guidance for the above case. 我感谢上述案例的任何提示和指导。

Thank you very much. 非常感谢你。

I didn't get your question. 我没收到你的问题。 But if you want to store in 6*6 character array without getting exception. 但是如果要以6 * 6字符数组存储而不会出现异常。 Here is the code how to do it 这是代码如何执行

    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    sc.close();
    s=s.replaceAll("\\s+", "");
    char []ch = s.toCharArray();
    char [][]charArray = new char[6][6];
    int i=0, j=0;

    for(int k=0;k<ch.length;k++)
    {


        if(i<=5 && j<=5)charArray[i][j] = ch[k];
        j++;
        if(j>5) {
            j=0;
            i++;
        }
        if(i>5 && j>5) break;

    }

The easiest way is to make sure the user has entered 36 characters exactly, then you can just copy the char array. 最简单的方法是确保用户正确输入了36个字符,然后可以复制char数组。

Live Demo 现场演示

Scanner scn = new Scanner (System.in);
System.out.print("String text: ");
String str = "";

// Read 6x6 characters
while (str.toCharArray().length != 36)
{
        System.out.print("Enter 36 characters: ");
        str = scn.nextLine().replaceAll("\\s+","");
}

char[] cArr = str.toCharArray();

// Print in 6x6 grid
for(int i=0; i<36; i++)
{
    System.out.print((i%6==0?"\n ":" ")+cArr[i]);
}
System.out.println();

And you could use that 1D array as a flat 2D array, or parse them into a 2D array in this way. 您可以将该1D数组用作平面2D数组,或以这种方式将它们解析为2D数组。

System.out.println("-------------------");

char[][] cMat = new char[6][6];

// Parse it into 6x6 matrix
for(int i=0; i<36; i++)
{
    cMat[i/6][i%6] = cArr[i];
}

// Print 6x6 matrix
for(int i=0; i<6; i++)
{
    for(int j=0; j<6; j++)
    {
        System.out.print(" "+cMat[i][j]);
    }
    System.out.println();
}

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

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