1

During an exercice of 2D arrays where first of all we put numbers as a strings in the array like that. And OK there is no problem, everything ok.

String [][] a = {
            { "0345" , "345" , "041" , "41" }, 
            { "0344" , "344" , "030" , "30" },
            { "0333" , "333" , "031" , "31" },
            { "0346" , "346" , "045" , "45" },
            { "0101" , "101" , "021" , "21" },
            { "0455" , "455" , "037" , "37" },
            { "0100" , "100" , "040" , "40" },             
    };
    for (int i = 0; i < c.length; ++i) {
        for(int j = 0; j < c[i].length; ++j) {
            System.out.print("|"+c[i][j]+"|");
        }
        System.out.println(" ");

So the out put it will show as these.

|0345||345||041||41| 
|0344||344||030||30| 
|0333||333||031||31| 
|0346||346||045||45| 
|0101||101||021||21| 
|0455||455||037||37| 
|0100||100||040||40| 

But when I did the same but changing the String for int, by mistake I foget to remove the 0 infront of the numbers. So here is the code.

int [][] b = {
            { 0345 , 345 , 041 , 41 }, 
            { 0344 , 344 , 030 , 30 },
            { 0333 , 333 , 031 , 31 },
            { 0346 , 346 , 045 , 45 },
            { 0101 , 101 , 021 , 21 },
            { 0455 , 455 , 037 , 37 },
            { 0100 , 100 , 040 , 40 }, 
    };
    for (int i = 0; i < b.length; ++i) {
        for(int j = 0; j < b[i].length; ++j) {
            System.out.print("|"+b[i][j]+"|");
        }
        System.out.println(" ");
    }

And the output it's something I didn't expect.

|229||345||33||41| 
|228||344||24||30| 
|219||333||25||31| 
|230||346||37||45| 
|65||101||17||21| 
|301||455||31||37| 
|64||100||32||40| 

So the thing is where I put 0345 the output it's 229 and so on. When I saw that, try in to figure out, I put all numbers in order from 0 to 19 by mean so in one line 0 in the other 00 so on util 019. But for another reason I don't know all number with 08 - 09 - 018 - 019 doesn't work. Here is the code of what I'm saying.

int[][] c = {
            { 010 , 10 , 00 , 0 },
            { 011 , 11 , 01 , 1 }, 
            { 012 , 12 , 02 , 2 },
            { 013 , 13 , 03 , 3 },
            { 014 , 14 , 04 , 4 },
            { 015 , 15 , 05 , 5 },
            { 016 , 16 , 06 , 6 },
            { 017 , 17 , 07 , 7 },            
    };

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

And here is the output.

|8||10||0||0| 
|9||11||1||1| 
|10||12||2||2| 
|11||13||3||3| 
|12||14||4||4| 
|13||15||5||5| 
|14||16||6||6| 
|15||17||7||7| 

So when you check the output, you realize that because of the error on the 08 - 09 (error or something I don't know) the 010 it's becaming 8.

Why is happening that ? There is an explanation for sure.

1 Answers1

1

That's because adding the 0 makes it an octal number. 345 octal is 229 decimal, for example. And as indicated in the comments, octal doesn't have 8 or 9.