如何根据无空格的输入字符串设置二维数组?

huangapple go评论72阅读模式
英文:

How to set 2 dim Array by input string without blank?

问题

这是我的代码:

Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
String[][] s = new String[size][size];

for (int row = 0; row < s.length; row++) {
    for (int col = 0; col < s[row].length; col++) {
        s[row][col] = sc.next();
    }
}
for (int row = 0; row < s.length; row++) {
    for (int col = 0; col < s[row].length; col++) {
        System.out.print(s[row][col] + " ");
    }
    System.out.println();
}

现在当我输入 3,然后输入数组值时:

3
a b c
a b c
a b c

输出将会是这样:

a b c
a b c
a b c

因此,我想知道如何修改代码,以便在我输入以下内容时获得相同的输出:

3
abc
abc
abc
英文:

This is my code:

    Scanner sc = new Scanner(System.in);
	int size = sc.nextInt();
	String[][] s = new String[size][size];

	for (int row = 0; row &lt; s.length; row++) {
		for (int col = 0; col &lt; s[row].length; col++) {
			s[row][col] = sc.next();
		}
	}
	for (int row = 0; row &lt; s.length; row++) {
		for (int col = 0; col &lt; s[row].length; col++) {
			System.out.print(s[row][col] + &quot; &quot;);
		}
		System.out.println();
	}

Now when i input 3, then input array value:

3
a b c
a b c
a b c

The output like this:

a b c
a b c
a b c

So I want to know how can i change the code to get same output when I input like this:

3
abc
abc
abc

答案1

得分: 1

试试这个。

int size = sc.nextInt();
String[][] s = new String[size][];

for (int row = 0; row < s.length; row++) {
    s[row] = sc.next().split("");
}
for (int row = 0; row < s.length; row++) {
    for (int col = 0; col < s[row].length; col++) {
        System.out.print(s[row][col] + " ");
    }
    System.out.println();
}
英文:

Try this.

int size = sc.nextInt();
String[][] s = new String[size][];

for (int row = 0; row &lt; s.length; row++) {
    s[row] = sc.next().split(&quot;&quot;);
}
for (int row = 0; row &lt; s.length; row++) {
    for (int col = 0; col &lt; s[row].length; col++) {
        System.out.print(s[row][col] + &quot; &quot;);
    }
    System.out.println();
}

答案2

得分: 0

java.util.Scanner.next()方法从扫描器中查找并返回下一个完整的标记。完整的标记是由匹配分隔符模式的输入前导和后续部分组成的。因此,扫描器使用分隔符模式将输入拆分为标记,默认情况下与空白字符匹配。
您必须使用某种分隔符。例如,您可以将分隔符设置为','

Scanner sc = new Scanner(System.in);
sc.useDelimiter(",");

然后您的输入应该像这样:

3,
a,b,c,
a,b,c,
a,b,c,

您的输出将是:

a b c 

a b c 

a b c 

在您的情况下,如果您想要这样的输入:

3
abc
abc
abc

然后逐个接收字符,您不能这样做,因为您必须指定一些分隔符,正如我所说,如果您不指定,默认分隔符将被采用。

然而,您可以编写一个绕过代码,类似于这样:

Scanner sc = new Scanner(System.in);
sc.useDelimiter("\\n");

int size = sc.nextInt();
String[][] s = new String[size][size];

for (int row = 0; row < s.length; row++) {
    String input = sc.next();
    for (int col = 0; col < s[row].length; col++) {
        s[row][col] = String.valueOf(input.toCharArray()[col]);
    }
}

for (int row = 0; row < s.length; row++) {
    for (int col = 0; col < s[row].length; col++) {
        System.out.print(s[row][col] + " ");
    }
    System.out.println();
}

然后您可以像这样传递输入:

3
abc
abc
abc

您应该会看到以下输出:

a b c 

a b c 

a b c 
英文:

The java.util.Scanner.next() method finds and returns the next complete token from this scanner. A complete token is preceeded and followed by input that matches the delimiter pattern. So, scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
You must use some kind of delimiter. For example, you can set delimiter to be ','

Scanner sc = new Scanner(System.in);
        sc.useDelimiter(&quot;,&quot;);

Then your input should be like this :

3,
a,b,c,
a,b,c,
a,b,c,

And Your output would be :

a b c 

a b c 

a b c 

In your case, if you want to take input like this:

3
abc
abc
abc

and then take characters one by one, you can't do it like that, beacause you must specify some delimiter, and as I said, if you don't the default delimiter will be taken.

However, you can write a workaround code, something like this :

Scanner sc = new Scanner(System.in);
        sc.useDelimiter(&quot;\\n&quot;);

        int size = sc.nextInt();
        String[][] s = new String[size][size];

        for (int row = 0; row &lt; s.length; row++) {
            String input = sc.next();
            for (int col = 0; col &lt; s[row].length; col++) {

                    s[row][col] = String.valueOf(input.toCharArray()[col]);
            }
        }


        for (int row = 0; row &lt; s.length; row++) {
            for (int col = 0; col &lt; s[row].length; col++) {
                System.out.print(s[row][col] + &quot; &quot;);
            }
            System.out.println();
        }

    }

Then You pass your input like this:

3
abc
abc
abc

And you should see the following output:

a b c 

a b c 

a b c 

答案3

得分: 0

以下是翻译好的部分:

这是我个人的看法,这也是最简单的方法。如果你想要,它还允许每一行的长度不同。

Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
// 清除之前的换行符
sc.nextLine();
// 初始化行数。
String[][] s = new String[size][];

// 输入行
for (int rows = 0; rows < size; rows++) {
    String row = sc.nextLine();
    // 在每个字符之间分割
    s[rows] = row.split("");
}

// 打印矩阵
for (String[] row : s) {
    System.out.println(String.join(" ", row));
}

对于输入:

3
abc
def
ghi

打印结果为:

a b c
d e f
g h i
英文:

Imho, this is the easiest way. It also allows for each row to be a different length if you want.

Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
// clear the previous new line
sc.nextLine();
//initialize the number of rows.
String[][] s = new String[size][];

// enter the rows
for (int rows = 0; rows &lt; size; rows++) {
	String row = sc.nextLine();
    // split between each character
	s[rows] = row.split(&quot;&quot;);
}

//print the matrix
for (String[] row : s) {
	System.out.println(String.join(&quot; &quot;,row));
}

For input

3
abc
def
ghi

Prints

a b c
d e f
g h i


</details>



huangapple
  • 本文由 发表于 2020年8月18日 18:52:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/63467092.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定