error: 不兼容的类型:String[] 无法转换为String

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

error: incompatible types: String[] cannot be converted to String

问题

以下是您代码的翻译部分:

  1. 我尝试在Java中使用字符串拆分函数接受一个6x6的矩阵输入输入方式如下并打印该矩阵
  2. 1 2 3 4 5 6
  3. 1 2 3 4 5 6
  4. 1 2 3 4 5 6
  5. 1 2 3 4 5 6
  6. 1 2 3 4 5 6
  7. 1 2 3 4 5 6
  8. 我得到的输出是
  9. Main.java:24: 错误不兼容的类型String[]无法转换为String
  10. c[j] = b[i].split(" ");
  11. 我的代码
  12. import java.util.*;
  13. import java.io.*;
  14. class Solution {
  15. public static void main(String args[]) {
  16. Scanner s = new Scanner(System.in);
  17. int a[][] = new int[6][6];
  18. String b[] = new String[6];
  19. for (int i = 0; i < 6; i++) {
  20. b[i] = s.nextLine();
  21. }
  22. // 初始化二维数组a[][]
  23. for (int i = 0; i < 6; i++) {
  24. for (int j = 0; j < 6; j++) {
  25. String c[] = new String[6];
  26. c[j] = b[i].split(" ");
  27. a[i][j] = Integer.parseInt(c[j]);
  28. }
  29. }
  30. // 打印输入数组
  31. for (int i = 0; i < 6; i++) {
  32. for (int j = 0; j < 6; j++) {
  33. System.out.print("\ta[i][j]\t");
  34. }
  35. }
  36. }
  37. }
  38. 请建议我如何克服这个错误

希望这有助于您理解并解决代码中的问题。如果您需要进一步的帮助,请随时提出。

英文:

I tried taking input of a 6 by 6 matrix in java using the string split function when the string is input in the following way, and to print the matrix.

  1. 1 2 3 4 5 6
  2. 1 2 3 4 5 6
  3. 1 2 3 4 5 6
  4. 1 2 3 4 5 6
  5. 1 2 3 4 5 6
  6. 1 2 3 4 5 6

The output that I get is

  1. Main.java:24: error: incompatible types: String[] cannot be converted to String
  2. c[j] = b[i].split(" ");

my code:

  1. import java.util.*;
  2. import java.io.*;
  3. class Solution {
  4. public static void main(String args[]) {
  5. Scanner s = new Scanner(System.in);
  6. int a[][] = new int[6][6];
  7. String b[] = new String[6];
  8. for (int i = 0; i < 6; i++) {
  9. b[i] = s.nextLine();
  10. }
  11. // initializing the 2d array a[][]
  12. for (int i = 0; i < 6; i++) {
  13. for (int j = 0; j < 6; j++) {
  14. String c[] = new String[6];
  15. c[j] = b[i].split(" ");
  16. a[i][j] = Integer.parseInt(c[j]);
  17. }
  18. }
  19. // printing the input array
  20. for (int i = 0; i < 6; i++) {
  21. for (int j = 0; j < 6; j++) {
  22. System.out.print("\ta[i][j]\t");
  23. }
  24. }
  25. }
  26. }

pls, suggest how I can overcome this error

答案1

得分: 0

split()函数的返回类型是数组类型。因为您要求Java将每个由" "(空格)分隔的值作为单独的值提供给您。所以Java将创建每个值的数组并返回给您。要存储数组,您需要一个数组类型的变量。在这里,c代表一个数组,但c[j]表示数组的单个索引。

您可以像这样更改您的代码:

  1. for (int i = 0; i < 6; i++) {
  2. String c[] = b[i].split(" ");
  3. for (int k = 0; k < c.length; k++) {
  4. a[i][k] = Integer.parseInt(c[k]);
  5. }
  6. }

输入是整数,您稍后将它们转换为整数,我建议您将输入直接作为整数接收,如下所示:

  1. class Solution {
  2. public static void main(String args[]) {
  3. Scanner s = new Scanner(System.in);
  4. int a[][] = new int[6][6];
  5. for (int i = 0; i < 6; i++) {
  6. for (int j = 0; j < 6; j++) {
  7. a[i][j] = s.nextInt();
  8. }
  9. }
  10. // 打印输入数组
  11. for (int i = 0; i < 6; i++) {
  12. for (int j = 0; j < 6; j++) {
  13. System.out.print("\ta[i][j]\t");
  14. }
  15. }
  16. }
  17. }
英文:

The return type of split() function is type of array. Because you are asking java to give me each value as separate which is separated by " " (space). So java will create an array of each value and returns you the array. For storing the array you need an variable of type array. here c represent an array, but c[j] represents an single index of the array.

You can change your code like:

  1. for (int i = 0; i < 6; i++) {
  2. String c[] = b[i].split(" ");
  3. for (int k = 0; k < c.length; k++) {
  4. a[i][k] = Integer.parseInt(c[k]);
  5. }
  6. }

The the inputs are integer and you are converting them to integer later, I would suggest you to take input as integer like below:

  1. class Solution {
  2. public static void main(String args[]) {
  3. Scanner s = new Scanner(System.in);
  4. int a[][] = new int[6][6];
  5. for (int i = 0; i < 6; i++) {
  6. for (int j = 0; j < 6; j++) {
  7. a[i][j] = s.nextInt();
  8. }
  9. }
  10. // printing the input array
  11. for (int i = 0; i < 6; i++) {
  12. for (int j = 0; j < 6; j++) {
  13. System.out.print("\ta[i][j]\t");
  14. }
  15. }
  16. }
  17. }

答案2

得分: 0

当我们调用String的split函数时,返回的是String[]。因此,类型为Stringc[j]不能等于String[]

下面的代码应该替换为:

  1. // 初始化2D数组a[][]
  2. for (int i = 0; i < 6; i++) {
  3. String[] c = b[i].split(" ");
  4. for (int j = 0; j < 6; j++) {
  5. a[i][j] = Integer.parseInt(c[j]);
  6. }
  7. }
英文:

When we call split function of String return the String[]. So c[j] (which is of type String) can't be equal to String[].

Below code should be replaced as:

  1. // initializing the 2d array a[][]
  2. for (int i = 0; i &lt; 6; i++) {
  3. String[] c = b[i].split(&quot; &quot;);
  4. for (int j = 0; j &lt; 6; j++) {
  5. a[i][j] = Integer.parseInt(c[j]);
  6. }
  7. }

huangapple
  • 本文由 发表于 2020年8月10日 12:01:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/63333986.html
匿名

发表评论

匿名网友

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

确定