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.