The section of my code that is commented out is what I need help with. I am wanting to check the user input of binary for anything other than a 1 or 0. Also, it will only accept an input of 10 numerical characters or less. I am wondering if there is a way to put a limit on the length of the input.

First things first though. Let's start with the commented section. Any ideas or changes? I appreciate the help.

package bintodecrec;

import java.util.*;

public class BinToDecRec
{

    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println();
        System.out.println("Enter 999 to exit program");
        System.out.println("Enter a binary number: ");
        String binary = scan.next();

        if((Integer.parseInt(binary) == 999))
        {
            System.out.println("Program ended.");
            System.exit(0);
        }
//        else if((Integer.parseInt(binary)) >= 2 || (Integer.parseInt(binary) < 0))
//        {
//            System.out.println("This is not a binary number. Please enter a number");
//            System.out.println("that contains only 1's and 0's.");
//            main(args);
//        }
        else
        {
            double decimal = 0;
            int temp;
            int i = 0;
            temp = Integer.parseInt(binary);

            while (temp != 0)
            {
                int r = temp % 10;
                double value = r * Math.pow(2, i);
                i++;
                decimal = decimal + value;
                temp /= 10;
            }
            System.out.println("Decimal of " + binary + " is " + decimal);
        }
    }//close main
}

Recommended Answers

All 2 Replies

Can anybody help with this??

You will have to check for each character in the input binary string to see if it is 0 or 1. Hope following example helps you,

import java.util.*;
 
public class BinToDecRec
{
 
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter 999 to exit program");
		System.out.println("Enter a binary number: ");
		String binary = scan.next();
		
		while((Integer.parseInt(binary) != 999)){

			boolean isBinary = true;

			//first convert the 'binary' string into a char array and check for each char whether it is zero or one
			char[] bits = binary.toCharArray();
			for(int j=0; j<bits.length; j++){
				if( (bits[j] != '0') && (bits[j] != '1') ){
					isBinary = false;
					break;
				}
			}

			if(!isBinary){//not binary
				System.out.println("This is not a binary number. Please enter a number");
		        System.out.println("that contains only 1's and 0's.");            
			}
			else{//binary
				double decimal = 0;
				int temp;
				int i = 0;
				temp = Integer.parseInt(binary);
	 
				while (temp != 0)
				{
					int r = temp % 10;
					double value = r * Math.pow(2, i);
					i++;
					decimal = decimal + value;
					temp /= 10;
				}
				System.out.println("Decimal of " + binary + " is " + decimal);
			}


			scan = new Scanner(System.in);
			System.out.println();
			System.out.println("Enter 999 to exit program");
			System.out.println("Enter a binary number: ");
			binary = scan.next();
		}

		System.out.println("Program ended.");
    }//close main
}

You can also use the Integer.parseInt(String s, int radix) function as,

import java.util.*;
 
public class BinToDecRec1
{
 
    public static void main(String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter 999 to exit program");
		System.out.println("Enter a binary number: ");
		String binary = scan.next();
		
		while((Integer.parseInt(binary) != 999)){

			try{
				int decimal = Integer.parseInt(binary, 2); //parse the string with radix 2. Throws NumberFormatException if not a valid boolean number else returns the decimal equivalent
				System.out.println("Decimal of " + binary + " is " + decimal);
			}
			catch(NumberFormatException nfe){
				System.out.println("This is not a binary number. Please enter a number");
	            System.out.println("that contains only 1's and 0's.");
			}

			scan = new Scanner(System.in);
			System.out.println();
			System.out.println("Enter 999 to exit program");
			System.out.println("Enter a binary number: ");
			binary = scan.next();
		}

		System.out.println("Program ended.");
    }//close main
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.