Home > Software design >  (java) convert a decimal number to binary without using parseint
(java) convert a decimal number to binary without using parseint

Time:07-18

I am new to java and I was learning how to convert from binary to decimal and vice versa. In the case of binary to decimal, I found out that I could use parseint, but I saw other methods that didn't use it, so I tried to implement them into my code, but it didn't work for me and I got stumped.

How would I be able to use a different method for calculating binary to decimal and implement it into my code?

Here is my code:

import java.util.Scanner;
class BinaryToDecimal {
    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        String binaryString;
        char choice;
        String nextLine = "Empty";
        int i = 0;
        choice = 'Y';
        try {
            do {


                System.out.print("Enter a binary number: ");
                binaryString = sc.nextLine();
                //Find the string count
                int count = binaryString.length();
                for (int j = 0; j< count; j  )
                {
                    if (binaryString.charAt(j) != '1' &&  binaryString.charAt(j) != '0')
                    {
                        System.out.print("Enter a binary number: ");
                        binaryString = sc.nextLine();
                        count = binaryString.length();
                        j=0;
                    }

                }
                i = Integer.parseInt(binaryString);

                if (i>0)
                    System.out.println("The decimal number is: "   Integer.parseInt(binaryString, 2));

                System.out.println("Continue using the calculator? Only input Y or N");
                String ln = sc.next();
                if(ln.length()==1){
                    choice = ln.charAt(0);
                }
                else{
                    choice = 'N';
                }
                if (sc.hasNextLine()) {
                    nextLine = sc.nextLine();

                }

            } while (choice == 'Y');


        } catch (NumberFormatException nfe) {
            System.out.println("Invalid input");
        }

    }
}

CodePudding user response:

Binary math involves adding 1 and multiplying by 2. I would use a regular expression to test if the input is valid. I would use an infinite loop and break when the user gives an answer besides y when prompted to continue. Putting that together, gives a simplified

Scanner sc = new Scanner(System.in);
while (true) {
    System.out.println("Enter a binary number: ");
    String binaryString = sc.nextLine();
    // An int value consists of up to 32 0 and 1s.
    if (!binaryString.matches("[01] ") || binaryString.length() > 32) {
        continue;
    }
    int v = 0;
    for (int i = 0; i < binaryString.length(); i  ) {
        v *= 2;
        if (binaryString.charAt(i) == '1') {
            v  ;
        }
    }
    System.out.println("The decimal number is: "   v);

    System.out.println("Continue using the calculator? Only input Y or N");
    String ln = sc.nextLine();
    if (!ln.equalsIgnoreCase("Y")) {
        break;
    }
}

CodePudding user response:

It looks like your missing you're missing the radix which the default I use is 2. Try this and let me know what happens

i = Integer.parseInt(binaryString,2); 
  • Related