0

I'm new to Java and had a question about summing together the digits of a single binary value in Java.

For example,

If the binary value has an even number of 1's, I would like to output the String "even" to another method. If there are an odd number of 1's, then I want to output the String value "odd".

Below is an example of my code:

    String eoro;
    String name = "1010101011";
    int num = Integer.parseInt(name, 2);
    System.out.println(num);
    if(num % 2 == 0)
        eoro = "even";
    else
        eoro = "odd";
    System.out.println(eoro);

The output comes up as

    683
    odd

I'm not sure if Integer.parseInt(name, 2); is the correct way of doing this, but I am unaware of another way of summing the digits of a single String value.

Please do not be rude and thanks for any help you can provide!

  • 1
    [`Integer.bitCount(num)`](https://docs.oracle.com/javase/8/docs/api/java/lang/Integer.html#bitCount-int-) if only to check with your own implementation. – Joop Eggen Sep 24 '17 at 19:45
  • Isn't `Integer.parseInt(name, 2)` "cheating". You aren't counting anything. You've parsed the string directly to a decimal value.... However, `683` is obviously odd. – OneCricketeer Sep 24 '17 at 19:49

4 Answers4

1

The shortest would be probably:

new BigInteger(value, 2).bitCount();

new BigInteger(value, 2) parses string as binary integer, bitCount() counts the bits. I'd use BigInteger as it is not clear, how long the string might be. You could use Integer or Long or other types if you're sure the string is short enough.

Alternatively you can just cound 1s in the string.

    int result = 0;
    for (char c: value.toCharArray()) {
        result += c == '1' ? 1 : 0;
    }
lexicore
  • 42,748
  • 17
  • 132
  • 221
0

Integer.parseInt is definitely not the right method. It will parse the given string to an integer, but an integer being odd or even is unrelated to the number of "1"s. E.g., consider the following:

public static void main(String[] args) {
    String eoro;
    String name = "10";
    int num = Integer.parseInt(name, 2);
    System.out.println(num);
    if(num % 2 == 0)
        eoro = "even";
    else
        eoro = "odd";
    System.out.println(eoro);
}

This will print

2
even

Which is not the output you want.

An easy way to count 1s in a string is to stream its characters and count them:

long numOnes = name.chars().filter(c -> c == '1').count();
Mureinik
  • 297,002
  • 52
  • 306
  • 350
0
public static void main(String[] args) {
    // declare variables
    String name = "1010101011";
    String eoro = (name.chars().filter(c -> c == '1').count() % 2 == 0)?"even":"odd";

    System.out.print(eoro);
    }
Shifat
  • 732
  • 1
  • 6
  • 20
-1

If the binary value has an even number of 1's, I would like to output the String "even"

Just because a binary value has an even number of 1's doesn't mean the decimal representation of that number be an even value, or num % 2 == 0. For example, if you have a 1 as the least significant value, as you have here, this will always make the value odd.

You can count using a loop, or like this.

int ones = name.replace("0", "").length();
if (ones % 2 == 0)
    System.out.println("even");
else
    System.out.println("odd"); 

Refer: Java: How do I count the number of occurrences of a char in a String?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245