简体   繁体   中英

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

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

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

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 < 6; i++) {
    String[] c = b[i].split(" ");
    for (int j = 0; j < 6; j++) {
        a[i][j] = Integer.parseInt(c[j]);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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