0

I'm trying to create a simple binary to decimal converter for my CS class to get a little extra credit. We only use Java in my class, so I'm making it on the Java platform. I've set up a homepage to call my class methods from to make it easier for me when I have multiple classes for the lessons I learn in the CS class. I'm calling my first method, stringSplit, which takes the binary value given as the argument, converts it to a String, then uses .substring to slice it up into each bit. It sets each bit to a variable to be later analyzed. Next, it runs through my bi2decFunctions method. This is what I have here:

public static void bi2decFunctions(String num128, String num64, String num32, String num16, String num8, String num4, String num2, String num1){
    int num128Return;
    int num64Return;
    int num32Return;
    int num16Return;
    int num8Return;
    int num4Return;
    int num2Return;
    int num1Return;

    System.out.println(" " + num128 + "   " + num64 + "   " + num32 + "   " + num16 + "   " + num8 + "   " + num4 + "   " + num2 + "   " + num1);
    //128
    if(num128 == "1"){
        num128Return = 128;
    } else {
        num128Return = 0;
    }
    //64
    if(num64 == "1"){
        num64Return = 64;
    } else {
        num64Return = 0;
    }
    //32
    if(num32 == "1"){
        num32Return = 32;
    } else {
        num32Return = 0;
    }
    //16
    if(num16 == "1"){
        num16Return = 16;
    } else {
        num16Return = 0;
    }
    //8
    if(num8 == "1"){
        num8Return = 8;
    } else {
        num8Return = 0;
    }
    //4
    if(num4 == "1"){
        num4Return = 4;
    } else {
        num4Return = 0;
    }
    //2
    if(num2 == "1"){
        num2Return = 2;
    } else {
        num2Return = 0;
    }
    //1
    if(num1 == "1"){
        num1Return = 1;
    } else {
        num1Return = 0;
    }
    System.out.println(" " + num128Return + "   " + num64Return + "   " + num32Return + "   " + num16Return + "   " + num8Return + "   " + num4Return + "   " + num2Return + "   " + num1Return);
    bi2decDisplay(num128, num64, num32, num16, num8, num4, num2, num1, num128Return, num64Return, num32Return, num16Return,
            num8Return, num4Return, num2Return, num1Return);
}

Each time I run this, the correct values for my 8-bit input shows up, but the num(1-128) return variables all equal 0. The 'if' statements aren't even affecting the outcome of them. Anyone have any idea?

I'd also like to present this to my CS class, so I'd like a 1-10 scale on the neatness and quality of my code.

Justin
  • 24,288
  • 12
  • 92
  • 142
Zulfe
  • 820
  • 7
  • 25

1 Answers1

4

Do not compare strings with ==, say equals:

str1.equals(str2)
Peter Pei Guo
  • 7,770
  • 18
  • 35
  • 54