0

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

Community
  • 1
  • 1
Apptrixie
  • 13
  • 1
  • 2
  • 1
    When we call split function of String return the String[]. So c[j] can't be equal to String[]. What you want to achieve here by splitting it? – Gaurav Jeswani Aug 10 '20 at 04:08

2 Answers2

0

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");
            }
        }
    }
}
Community
  • 1
  • 1
Deepak Kumar
  • 1,246
  • 14
  • 38
0

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]);
    }
}
Community
  • 1
  • 1
Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47