-2

I was practicing Java and this time I was making a program that,
1. Converts 2 Binary numbers to Decimal
2. Calculates the sum of those 2 Decimal numbers
3. Shows both the result in Binary and in Decimal
I know that I'm not doing the efficient way in the code but that is on purpose.
(So I can practice more with only one project)
But there is a problem in my code that when it is going to convert from Binary to Decimal
it always displays the first number as "3" and the second one as "2".
Could someone tell me what I'm doing wrong? Thanks in advance.
(I already tried searching on the internet the answer, but I couldn't find it.
Sorry if I am wasting your time with a dumb mistake.)

package binary2decimal2binary;
import java.util.Scanner;
import java.util.ArrayList;

public class Binary2Decimal2Binary {
private static long binary1;
private static long binary2;
private final static ArrayList POWERSOF2 = new ArrayList<Integer>();
private final static ArrayList<Integer> SEPARATED2 = new ArrayList<Integer>();
private static int temp1 = 0;
private static int result1 = 0;
private static int temp2 = 0;
private static int result2 = 0;
private final static ArrayList<Integer> SEPARATED1 = new ArrayList<Integer>();



static void binary2ArrayList(long binary1, long binary2){
String separating1 = String.valueOf(binary1);
System.out.println("Debugger 1 :" + separating1);
for(int i = 0; i < separating1.length(); i++) {
    int j =(int) Character.digit(separating1.charAt(i), 10);
    SEPARATED1.add(j);}
 String separating2 = String.valueOf(binary2);
 System.out.println("Debugger2 :" + separating2);
 for(int x = 0; x < separating2.length(); x++) {
     int y =(int) Character.digit(separating2.charAt(x), 10);
     SEPARATED2.add(y);
 }
 Arraylist2Decimal();

}



static void Arraylist2Decimal(){

   for(Integer intValue  : SEPARATED1 ){
   int i = 0;
   temp1 = (int) SEPARATED1.get(i);
   if(temp1 == 1){
       result1 = (int) POWERSOF2.get(i) + result1;

   }
   i += 1;
   System.out.println("Debugger 3:" + i);
   }

   for(Integer intValue: SEPARATED2){
   int k = 0;
   temp2 = (int) SEPARATED2.get(k);
   if(temp2 == 1){

   result2 = (int) POWERSOF2.get(k) + result2;

   } k += 1;
   System.out.println("Debugger 4:" + k);
           }
};



    public static void main(String[] args) {
        POWERSOF2.add(1);
        POWERSOF2.add(2);
        POWERSOF2.add(4);
        POWERSOF2.add(8);
        POWERSOF2.add(16);
        POWERSOF2.add(32);
        POWERSOF2.add(64);
        POWERSOF2.add(128);
        POWERSOF2.add(256);
        POWERSOF2.add(512);
        POWERSOF2.add(1024);
        POWERSOF2.add(2048);
        POWERSOF2.add(4096);
        POWERSOF2.add(8192);
        POWERSOF2.add(16384);
        POWERSOF2.add(32768);

       Scanner scan = new Scanner(System.in);

 System.out.println("Input first binary number.");
 binary1 = scan.nextLong();
 System.out.println("Input second binary number.");
 binary2 = scan.nextLong();
 scan.close();
 binary2ArrayList(binary1,binary2);

 System.out.println("First Number Converted:" + result1);
 System.out.println("Second Number Converted:" + result2);



    }



}

EDIT : I added some code to check what is happening with my loops, and something weird was happening.
INPUT:
101
10
OUTPUT:
Debugger 1 :101
Debugger2 :10
Debugger 3:1
Debugger 3:1
Debugger 3:1
Debugger 4:1
Debugger 4:1
First Number Converted:3
Second Number Converted:2
Even though I added
i += 1; and
k += 1; in the loops the values is never
going up. Please help me.

Dynami
  • 35
  • 7
  • Integer is already a predefined class, so it is an invalid variable name. – Cardinal System Jul 11 '17 at 03:51
  • @CardinalSystem That is not true. Try it. Java will let you declare a variable named `Integer`. – ajb Jul 11 '17 at 03:59
  • Well that's stupid O.o – Cardinal System Jul 11 '17 at 04:00
  • `powersOf2`, `separated1`, and `separated2` should be declared as `ArrayList`, not `ArrayList`. If you just make them `ArrayList`, then the compiler knows only that the elements of the list are `Objects`. It doesn't know that they're `Integers`. – ajb Jul 11 '17 at 04:02
  • @ajb oh I didn't know about that. I thought that when i wrote "= new Arraylist" the compiler would know that it was a Integer Arraylist. Thanks for the advice! – Dynami Jul 12 '17 at 04:09
  • When you use a variable, Java only cares what type you _declared_ it as (the type that you put to the left of the variable). The expression you use to initialize it doesn't matter. – ajb Jul 12 '17 at 04:26

2 Answers2

0

There are actually a bunch of things here that need some improvements.

  • Since Java 1.5 there are Generics. Use them wherever possible (i.e. ArrayList<Integer> instead of the raw type ArrayList).

  • Rather use the interfaces than the implementations when defining your variables (i.e. List<Integer> list = new ArrayList<Integer>(); instead of ArrayList<Integer> list = new ArrayList<Integer>();).

  • Don't overuse the static keyword. Make use of OOP concepts like classes and encapsulation. It prevents you from unwanted side effects in the long run.

  • Try to add private and final to as many fields as possible. It prevents reassignment and other classes from accessing those values when they have nothing to do with it.

  • You for loops need to be improved: for(Type element : Iterable) already gives you the elements in that iterable. Calling separated1.get(i) in that loop makes no sense (especially since i is always 0), because the single elements are already accessible via element.

Putting it all together, your code should look something like this:

....

private final List<Integer> separated2 = new ArrayList<Integer>();

....

for(Integer intValue : separated2) {
    // your logic using intValue
    ....
}

EDIT: An equivalent example with a traditional for loop would look like this:

private final List<Integer> separated2 = new ArrayList<Integer>();

....

for(int i = 0; i < separated2.size(); i++) {
     Integer intValue = separated2.get(i);
     // your logic using intValue
    ....
}
QBrute
  • 4,078
  • 6
  • 32
  • 38
  • Thanks for the advices. But i just need to know one more thing. Could you tell me what the "intValue" is? Is it just a int variable that you just initialized or is it the number of content in the ArrayList? – Dynami Jul 12 '17 at 01:30
  • @Dynami `intValue` is just the name I've given to this variable. You can read it as *"For each Integer with the name 'intValue' in the collection 'separated2', do the following..."*. For more information you can visit [this SO question](https://stackoverflow.com/questions/85190/how-does-the-java-for-each-loop-work) which has some pretty good answers on this topic. Also, I've added some more information in my answer. – QBrute Jul 12 '17 at 04:38
0

To convert binary string to Integer use :

Integer.parseInt("binaryString",2);

Here is working code.

public static void main(String[] args) throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Input first binary number.");
        String firstBinary = scanner.next("[0-1]+");
        System.out.println("Input second binary number.");
        String secondBinary = scanner.next("[0-1]+");

        int int1 = convertToInt(firstBinary);
        int int2 = convertToInt(secondBinary);

        int sum = calculateSum(int1, int2);

        display(firstBinary, secondBinary, int1, int2);

        System.out.println("sum is " + sum);
    }

    public static void display(String binary1, String binary2, int arg1, int arg2) {
        System.out.printf("%1s10 = %2d5\n%3s10 = %4d5", binary1, arg1, binary2, arg2);
    }

    public static int convertToInt(String binary) {
        return Integer.parseInt(binary, 2);
    }

    public static int calculateSum(int arg1, int arg2) {
        return arg1 + arg2;
    }

This program first asks you to enter two binaries. The it converts them to Integer and calculates their sum. Finall it displays results to console.

Jon Clements
  • 132,101
  • 31
  • 237
  • 267
Jay Smith
  • 2,201
  • 3
  • 13
  • 26