0

I created simple decimal to binary method like this :

public static int[] convertDectoB(int number, int size) {
    int binary[] = new int[size];
    for (int a = number; a > 0 && size > 0; size--, a = a / 2) {
        binary[size - 1] = a % 2;
    }
    return binary;
}

I asked user to enter numbers like that :

"Enter numbers : "

4-2-7.

The aim is convert these 3 numbers into a binary and print it :

1 0 0

0 1 0

1 1 1

I also created numberFinder method to extract integers into the String that user entered :

public static int[] numberFinder(String numbers, int size) {
    int numaraa[] = new int[size];
    int a = 0;
    for (int i = 0; i < numaraa.length; i++) {
        for (; a < numbers.length(); a++) {
            if (numbers.charAt(i) == '-')
                i++;
            else if (a == 0) {
                if (Character.isDigit(numbers.charAt(a)))
                    numaraa[i] += numbers.charAt(a);
            }
            else {
                if (Character.isDigit(numbers.charAt(a)))
                    numaraa[i] += numbers.charAt(a);
            }
        }
    }
    return numaraa;
}

In the end , I created 2D array and I wanna implement these binary values :

for (int i = 0; i < mainarray.length; i++) {
        for (int j = 0; j < mainarray[i].length; j++) {
            mainarray[i][j] = ? 
        }
    }
}

But I can't implement all binary values for each array block.

Lavish Kothari
  • 2,211
  • 21
  • 29
  • For your `numberFinder`, you can use `string.split("-")` instead. – MWB Dec 10 '18 at 17:59
  • Integer::toBinaryString() should work if your aim is to print the values. – Bill K Dec 10 '18 at 18:02
  • "MWB" ty for your advice but we did not learned split method in the class so I can't use it. –  Dec 10 '18 at 18:09
  • Unfortunately we can't use ready methods like toBinaryString(). We have to create our own methods to run the code. –  Dec 10 '18 at 18:11

1 Answers1

0

If you need to make your own toBinaryString() method there are a lot of examples online, such as borrowing the logic from this answer Print an integer in binary format in Java:

public static String intToString(int number) {
        StringBuilder result = new StringBuilder();

        for(int i = 2; i >= 0 ; i--) {
            int mask = 1 << i;
            result.append((number & mask) != 0 ? "1" : "0");
        }
        return result.toString();
    }

Note: I modified this method to only hold 3 digit binary codes, so the highest integer it can convert is 7. However if you want to convert larger integers, say up to 16, or 32, you'll need to increase the counter in the for-loop, which I'll leave that for you to decide.

For each line in the user input, split it on the hyphen character '-':

codes = line.split("-");
ListofCourses.addAll(Arrays.asList(codes));
...

Then call this method on your ListofCourses:

for(int r = 0; r < ListofCourses.size(); r++){
    System.out.println(intToString(Integer.parseInt(ListofCourses.get(r))));
}
ListofCourses.clear();

Then should get expected output. Here's demo:

Ron's Copy

Enter 3 collections of course codes one collection per line
4-2-7

Size: 3 Sorted: [4, 2, 7]
100
010
111