Java: I don't know how to reject binary values longer than 32 bits [USER INPUT]

As the title says. I am designing a program in-order convert Binary to Decimal values with added features:

Rejecting binary values longer than 32 bits

Prompting the user to make multiple entries after completing the binary to decimal conversion of their first entry.

I was trying to code this in Nested For Loops, but I don't know if I've really done that.

I own the latest Java Programming 9th edition by Y. Daniel Liang. I don't find this book very helpful.

Here is what i have so far. Help is appreciated!(:

Code :

public class BinaryToDecimal {
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
String binary;
int decimal=0b10, i, rem;
boolean isBinary = true;
JOptionPane.showInputDialog(null, "How many bit sequences do you want to convert to decimal?");
binary = JOptionPane.showInputDialog(null, "Enter a string of bits(no spaces)");
decimal = Integer.parseInt(binary);
char[] bits = binary.toCharArray();
for(i = 0; i < bits.length; i++) {
if( (bits[i] != '0') && (bits[i] != '1') ){
isBinary = false;
}
}
if(!isBinary){
JOptionPane.showMessageDialog(null, "This is not a binary number.");
JOptionPane.showMessageDialog(null, "Please enter a string of only 1's and 0's.");
}
else{
i = decimal / 2;
rem = decimal % 2;
i = Integer.parseInt(binary,2);
JOptionPane.showMessageDialog(null, " The decimal of " + binary + " is " + i);
System.exit (0);
}
}
}

Re: Java: I don't know how to reject binary values longer than 32 bits [USER INPUT]

Quote:

Rejecting binary values longer than 32 bits

-actually you did not meet that condition in your program.

since you parse your input to integer, therefore the program treated the input as integer of base 10 (decimal) not base 2(binary)

I think it will be better if you just store it in string, then check if the input's length is less than or equal

to 32 (cause you want 32 bits) and then convert it to decimal.

(you just have to analyze the pattern of converting binary to decimal) :)

Quote:

Prompting the user to make multiple entries after completing the binary to decimal conversion of their first entry.

you must put the statements that's getting the user's input in a loop.

with a condition that will terminate it if the user wants.