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

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

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

问题

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

我尝试在Java中使用字符串拆分函数接受一个6x6的矩阵输入输入方式如下并打印该矩阵

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: 错误不兼容的类型String[]无法转换为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();
        }

        // 初始化二维数组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]);
            }
        }

        // 打印输入数组
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                System.out.print("\ta[i][j]\t");
            }
        }
    }
}
请建议我如何克服这个错误

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

英文:

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 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

The output that I get is

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

my code:

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

pls, suggest how I can overcome this error

答案1

得分: 0

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

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

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

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

class Solution {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        int a[][] = new int[6][6];

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                a[i][j] = s.nextInt();
            }
        }

        // 打印输入数组
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                System.out.print("\ta[i][j]\t");
            }
        }
    }
}
英文:

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:

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

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

class Solution {
    public static void main(String args[]) {
        Scanner s = new Scanner(System.in);
        int a[][] = new int[6][6];

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                a[i][j] = s.nextInt();
            }
        }

        // 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");
            }
        }
    }
}

答案2

得分: 0

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

下面的代码应该替换为:

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

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:

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

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:

确定