Error in 3x3 matrix

Asked

Viewed 189 times

0

I’m having difficulties in a java code. I’m trying to make a code that generates a 3x3 matrix. I made a code that at first glance works everything Okay.

matriz3x3

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

however when trying to put 2 numbers in the field which will fill the matrix, for example

3x3 matrix

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

gives a big error and no longer works the code, I am looking for my error, but I am new in Java and ask for the help of vcs. 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)

  • What error? Add the stack of errors in the question.

  • The error that occurs is that the code simply does not work anymore, if put equal to the first code I put there, will generate the matrix and everything, but when I put equal to the second matrix nor generate more. .

  • I edited and put the errors above

  • It is not clear your problem. You say you error to "2 numbers in the field" but your code only lists the array elements. Which field? Edit the question and provide code that will codify the problem, because the code doesn’t have the problem you say it faces.

1 answer

1

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

Doing like 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, a 3-dimensional vector is required, and convert a character vector to String, using the valueOf. That’s how it works:

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();
    }
}
  • I understand, I thank you for your help. Because I am new in this language I did not know this little difference. thankful

Browser other questions tagged

You are not signed in. Login or sign up in order to post.