0

I was wondering if it is possible to make a loop for an array of strings that i want to evaluate in my code. I want to do more than one binary number at a time. So far I have this working correctly, however I cant figure our how to make it evaluate more than one binary number at a time. Thank you.

 package twoComplement;

public class bintodec {

public static void main (String[] args)throws java.io.IOException {

int number,
    digit1,
    digit2,
    digit3,
    digit4,
    digit5,
    digit6,
    digit7,
    digit8,
    result;

String num = "11100111";

number = Integer.parseInt(num);

digit1 = ((number % 100000000) - (number % 10000000 % 10000000)) / 10000000;
digit2 = ((number % 10000000) - (number % 10000000 % 1000000)) / 1000000;
digit3 = ((number % 1000000) - (number % 1000000 % 100000)) / 100000;
digit4 = ((number % 100000) - (number % 100000 % 10000)) / 10000;
digit5 = ((number % 10000) - (number % 10000 % 1000)) / 1000;
digit6 = ((number % 1000) - (number % 1000 % 100)) / 100;
digit7 = ((number % 100) - (number % 100 % 10)) / 10;
digit8 = (number % 10);

result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1);

System.out.println ( "Binary number: " + num + "\nDecimal Number: " + result);
System.out.println();
System.exit( 0 );

}
}

5 Answers 5

4

Yes, this is the purpose of loops ;-) An advanced for loop is the best way to iterate through an array. You can also iterate through collections (IE: ArrayList), which makes it easier to add new items.

String[] numbersToEvaluate = new String[]{"11100111", "100101", "10101101"};

for (String num: numbersToEvaluate)
{
    number = Integer.parseInt(num);

    digit1 = ((number % 100000000) - (number % 10000000 % 10000000)) / 10000000;
    digit2 = ((number % 10000000) - (number % 10000000 % 1000000)) / 1000000;
    digit3 = ((number % 1000000) - (number % 1000000 % 100000)) / 100000;
    digit4 = ((number % 100000) - (number % 100000 % 10000)) / 10000;
    digit5 = ((number % 10000) - (number % 10000 % 1000)) / 1000;
    digit6 = ((number % 1000) - (number % 1000 % 100)) / 100;
    digit7 = ((number % 100) - (number % 100 % 10)) / 10;
    digit8 = (number % 10);

    result = (digit1 * -128) + (digit2 * 64) + (digit3 * 32) + (digit4 * 16) + (digit5 * 8) + (digit6 * 4) + (digit7 * 2) + (digit8 * 1);

    System.out.println ( "Binary number: " + num + "\nDecimal Number: " + result);
}
Sign up to request clarification or add additional context in comments.

1 Comment

Worked perfectly, Thank you very much. I will mark as the answer when possible.
1

This is the way to define string array:

int n = 10;
String[] arr = new String[n]; 

And here is how to iterate over array:

for (int i = 0; i < n; i++) {
    arr[i] = "element number " + i;
}

or this way:

for (String element : arr) {
    System.out.println(element);
}

Comments

1

Do you mean something like

for (String num : "11100111,1010,11111,110101010101010101011101010101010101010101".split(",")) {
    long result = Long.parseLong(num, 2);
    System.out.println("Binary number: " + num + ", Decimal Number: " + result);
}

prints

Binary number: 11100111, Decimal Number: 231
Binary number: 1010, Decimal Number: 10
Binary number: 11111, Decimal Number: 31
Binary number: 110101010101010101011101010101010101010101, Decimal Number: 3665040856405

2 Comments

The built-in Integer.parseInt() conversion is nearly cheating :)
@ggrigery It shouldn't be a problem unless this is [homework] ;)
1

If you are running at least Java 5, you can use a for-each construct to loop over any arbitrary Array or Collection.

String[] nums = //init
for (String num:nums) {
    // Do work.
}

If, however, you are on Java 1.4 or earlier, or you care about the index of the array, you need to use a traditional for loop

String[] nums = //init
for (int i = 0; i<nums.length; i++) {
    String num = nums[i];
    // Do work.
}

Comments

0

Yep, that's pretty easy.

String[] array = {"first", "second", "third"};
for (String element : array) {
   // do whatever
}

As an aside, however, you may wish to consider the following for clearer code.

String num = "1000";
int digit1 = Character.digit(num.charAt(0), 2);
// if you are only expecting a binary number then the second argument (2)
// tells the digit method to throw an error if it gets a digit other than 0 or 1
...

If this is for a school exercise or similar you may wish to test your code against the builtin Integer parser.

int numFromBinaryString = Integer.parseInt(numberString, 2);
// again, here the second argument tells parseInt to interpret numberString as
// binary string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.