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
}