3

Say I have an Integer, I convert it to binary string first.

            int symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);

In this case, I would have symptomsBit as 1101100011 in binary.

But how can I further convert it to Int Array that has the same content, such as
symptomsBitArr[] = {1,1,0,1,1,0,0,0,1,1}?

Okay. Here is what I have tried. I know symptomsBit.split(" ") isn't correct. But do not know how to further improve it.

            symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);
            String[] symptomsBitArr = symptomsBit.split(" ");
            System.out.println("symptomsBit: " + symptomsBit);
            System.out.println("symptomsBitArray: " + symptomsBitArr);
            int[] symptomsArray = new int[symptomsBitArr.length];
            for (int i = 0; i < symptomsBitArr.length; i++) {
                 symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
                 System.out.println("symptomsArray: " + symptomsArray);
                }

I tried the way Idos suggested as below:

            symptomsM = 867;
            String symptomsBit = Integer.toBinaryString(symptomsM);
            String[] symptomsBitArr = symptomsBit.split(" ");
            System.out.println("symptomsBit: " + symptomsBit);
            System.out.println("symptomsBitArray: " + symptomsBitArr);
            int[] symptomsArray = new int[symptomsBitArr.length];
            for (int i = 0; i < symptomsBitArr.length; i++) {
                 //symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
                 symptomsArray[i] = Integer.parseInt(String.valueOf(symptomsBit.charAt(i)));
                 System.out.println("symptomsArray: " + symptomsArray);
                }

But it still not works. Here is the output:

symptomsBitArray: [Ljava.lang.String; @2a139a55
symptomsArray: [I@15db9742
Orangeblue
  • 229
  • 1
  • 5
  • 15
  • Have you actually tried at all to write code for this? Did you run into some sort of problem? – John Bollinger Jan 29 '16 at 20:01
  • Just added the code that I have tried. – Orangeblue Jan 29 '16 at 20:03
  • It is interesting someone suddenly downvoted almost every question I have asked. Well, I am not taking any CS class, and trying to learn it by myself. If this bothers any of you "expert" out there, sorry for that. I do appreciate every help and answer from the real experts. – Orangeblue Jan 29 '16 at 20:27
  • If you were serial down voted the system will likely pic it up and roll them back; if not you could flag one for moderation attention. It could be just a bad day too.\ – ChiefTwoPencils Jan 29 '16 at 20:33
  • Not my downvote, but if your questions are consistently being downvoted, then you should consider consulting the help page about [how to ask questions here](http://stackoverflow.com/help/how-to-ask). – John Bollinger Jan 29 '16 at 20:33
  • Thanks John and Chief. Still learning it, and sometimes do make silly mistakes. – Orangeblue Jan 29 '16 at 20:39

3 Answers3

1

I think this should do the trick:

String symptomBit = "1010";
for(int i = 0; i < symptomsBitArr.length; i++) {
    symptomsBitArr[i] = Integer.parseInt(String.valueOf(symptomsBit.charAt(i)));
}

// symptomsBitArr = [1,0,1,0]

// print array here
for (int j = 0; j < symptomsBitArr.length; j++) {
    System.out.println("symptomsArray: " + symptomsArray[j]);
}
Idos
  • 15,053
  • 14
  • 60
  • 75
0

I know symptomsBit.split(" ") isn't correct. But do not know how to further improve it.

Yes, you're almost there but must have made a few minor mistakes.

For one, that split() isn't correct because you're trying to split it on " "s but what you need is ""s because it's a continuous string.

That will give you the array that you think you're getting. But you also don't print the elements right. You should index into the array to get the element to print. Like so...

int symptomsM = 867;
String symptomsBit = Integer.toBinaryString(symptomsM);
String[] symptomsBitArr = symptomsBit.split("");
System.out.println("symptomsBit: " + symptomsBit);
System.out.println("symptomsBitArray: " + symptomsBitArr);
int[] symptomsArray = new int[symptomsBitArr.length];
for (int i = 0; i < symptomsBitArr.length; i++) {
    symptomsArray[i] = Integer.parseInt(symptomsBitArr[i]);
    System.out.println("symptomsArray: " + symptomsArray[i]);
} 
ChiefTwoPencils
  • 13,548
  • 8
  • 49
  • 75
0

I am guessing you need each of value of the binary string to be of type int.

 String binString = Integer.toBinaryString(N);
 char[] intBinaryArray = binString.toCharArray();

Then while looping, you can easily do:

   for (int i = 0; i < binString.length() - 1; i++) {
            if (Character.getNumericValue(intBinaryArray[i]) 
Amos Kosgei
  • 877
  • 8
  • 14