resu
resu

Reputation: 321

java one dimensional string to multidimensional

I am given a string that can represent hex values and am told to convert it to such and store it in a string:

String str = new String("FF E7 C3 E7 FF");
String[] arrayStr = str.split(" ");

Then I need to convert each hex value to its binary equivalent:

String[] arrayBin = new String[arrayStr.length];
for (int i = 0; i < arrayStr.length; i++) {
        arrayBin[i] = Integer.toBinaryString(Integer.parseInt(arrayStr[i],16));
}

This is where I get lost. Is this binary string technically one dimensional or two dimensional? Basically, I need to convert it to a two dimensional array so that I can iterate through it in both x and y directions, to perform something like this:

for (int k = 0; k < arrayBin.length; k++) {
    for (int l = 0; l < arrayBin[k].length; l++) {
        if (arrayBin[k][l] == "1") {
            arrayBin[k][l] = "x";
        } else {
            arrayBin[k][l] = " ";               
        }
    }
}

At this point I get nothing but compiler errors, and despite all the articles online about multidimensional arrays, no search has given me something that works. I have tried things like:

String[][] arrayBin2 = new String[arrayBin.length][]
for (int m = 0; m < arrayBin.length; m++) {
    arrayBin2[m][] = arrayBin[m];
}

But that did not work. Any help or suggestions as to what I am doing wrong would be greatly appreciated.


Second attempt in response to @Eran 's suggestions:

public class Challenge3 {
    public static void main(String args[]) {
        String str = new String("FF E7 C3 E7 FF");
        String[] arrayStr = str.split(" ");
        String[] arrayBin = new String[arrayStr.length];
        for (int i = 0; i < arrayStr.length; i++) {
            arrayBin[i] = Integer.toBinaryString(Integer.parseInt(arrayStr[i],16));
        }

        for (int k = 0; k < arrayBin.length; k++) {
            StringBuilder sb = new StringBuilder(arrayBin[k].length());     
            for (int l = 0; l < arrayBin[k].length(); l++) {
                if (arrayBin[k].charAt(l) == 1) {
                    sb.append("x");
                } else {
                    sb.append(" ");            
                }
            }
            arrayBin[k] = sb.toString();
            System.out.println(arrayBin);
        }
    }
}

As a side note, the code is supposed to print out the following when done correctly:

xxxxxxxx
xxx  xxx
xx    xx
xxx  xxx
xxxxxxxx 

Upvotes: 1

Views: 146

Answers (1)

Eran
Eran

Reputation: 393781

Your arrayBin is 1-dimentional.

If you want to iterate over the bits of each binary String, use the methods length() and charAt() of the String class :

for (int k = 0; k < arrayBin.length; k++) {
    StringBuilder sb = new StringBuilder(arrayBin[k].length());
    for (int l = 0; l < arrayBin[k].length(); l++) {
        if (arrayBin[k].charAt(l) == '1') {
            sb.append ('x');
        } else {
            sb.append (' ');            
        }
    }
    arrayBin[k] = sb.toString();
    System.out.println(arrayBin[k]);
}

Output :

xxxxxxxx
xxx  xxx
xx    xx
xxx  xxx
xxxxxxxx

Note that you can't change the String instances in your arrayBin array, since String is immutable. What you can do is construct a new String and replace each String in the arrayBin array with the new String.

Upvotes: 1

Related Questions