【发布时间】:2020-08-10 04:01:40
【问题描述】:
当以以下方式输入字符串时,我尝试使用字符串拆分函数在java中输入6乘6矩阵,并打印矩阵。
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
1 2 3 4 5 6
我得到的输出是
Main.java:24: error: incompatible types: String[] cannot be converted to String
c[j] = b[i].split(" ");
我的代码:
import java.util.*;
import java.io.*;
class Solution {
public static void main(String args[]) {
Scanner s = new Scanner(System.in);
int a[][] = new int[6][6];
String b[] = new String[6];
for (int i = 0; i < 6; i++) {
b[i] = s.nextLine();
}
// initializing the 2d array a[][]
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
String c[] = new String[6];
c[j] = b[i].split(" ");
a[i][j] = Integer.parseInt(c[j]);
}
}
// printing the input array
for (int i = 0; i < 6; i++) {
for (int j = 0; j < 6; j++) {
System.out.print("\ta[i][j]\t");
}
}
}
}
请建议我如何克服这个错误
【问题讨论】:
-
当我们调用String的split函数返回String[]。所以 c[j] 不能等于 String[]。你想通过拆分在这里达到什么目的?
标签: java multidimensional-array split string-formatting tostring