Error in matrix 3x3

0

I'm having trouble with a java code. I am trying to make a code which generates a 3x3 array. I made a code which at first sight works okay.

matrix3x3

    public static void main(String[] args) {
    // TODO code application logic here
        char[][] ch = {{'2','1','9'}, {'4','8','0'},{'7','5','3'}};

for (int i = 0; i < ch.length; i++) {
    for (int j = 0; j < ch[i].length; j++) {
        System.out.print("ch["+i+"]["+j+"] = "+ch[i][j]+"\t");
    }
    System.out.println();
}}

But try to put 2 numbers in the field to fill the array, for example

3x3 array

    public static void main(String[] args) {
    // TODO code application logic here
        char[][] ch = {{'20','10','90'}, {'40','80','01'},{'70','50','30'}};

for (int i = 0; i < ch.length; i++) {
    for (int j = 0; j < ch[i].length; j++) {
        System.out.print("ch["+i+"]["+j+"] = "+ch[i][j]+"\t");
    }
    System.out.println();
}}

There is a big mistake and the code no longer works, I'm looking for my error, but I'm new to Java and I ask for your help. The errors that appear are: at java.lang.ClassLoader.defineClass1 (Native Method)     at java.lang.ClassLoader.defineClass (ClassLoader.java:763)     at java.security.SecureClassLoader.defineClass (SecureClassLoader.java:142)     at java.net.URLClassLoader.defineClass (URLClassLoader.java:467)     at java.net.URLClassLoader.access $ 100 (URLClassLoader.java:73)     at java.net.URLClassLoader $ 1.run (URLClassLoader.java:368)     at java.net.URLClassLoader $ 1.run (URLClassLoader.java:362)     at java.security.AccessController.doPrivileged (Native Method)     at java.net.URLClassLoader.findClass (URLClassLoader.java:361)     at java.lang.ClassLoader.loadClass (ClassLoader.java:424)     at sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java:335)     at java.lang.ClassLoader.loadClass (ClassLoader.java:357)     at sun.launcher.LauncherHelper.checkAndLoadMain (LauncherHelper.java:495)

    
asked by anonymous 10.01.2018 / 19:56

1 answer

1

When you use type char you can only use one character, such as 1, 2, 3, enclosed in single quotation marks, '1', '2', '3' . To do with more characters you could use type String , such as String[] numeros = {"10", "20", "30"} . To do the same with characters, you could use another approach, more or less this: char[][] numeros = {{'1', '0'}, {'2', '0'}, {'3', '0'}} , where each row represents a number.

Making String would look like this:

public static void main(String[] args) {
            // TODO code application logic here
        String[][] ch = {{"20","10","90"}, {"40","80","01"},{"70","50","30"}};

        for (int i = 0; i < ch.length; i++) {
            for (int j = 0; j < ch[i].length; j++) {
                System.out.print("ch["+i+"]["+j+"] = "+ch[i][j]+"\t");
            }
            System.out.println();
        }
    } 

To use as a char, you need a 3-dimensional vector, and convert a vector of characters to String, using valueOf . This approach looks like this:

public static void main(String[] args) {
        // TODO code application logic here
    char[][][] ch = {
        {
            {'2', '0'},{'1', '0'},{'9', '0'}
        }, 
        {
            {'4', '0'},{'8', '0'},{'0', '1'}
        },
        {
            {'7', '0'},{'5', '0'},{'3', '0'}
        }
    };

    for (int i = 0; i < ch.length; i++) {
        for (int j = 0; j < ch[i].length; j++) {
            System.out.print("ch["+i+"]["+j+"] = "+String.valueOf(ch[i][j])+"\t");
        }
        System.out.println();
    }
}
    
10.01.2018 / 20:16