'stuck on java program converting user input string representing binary number to decimal
Java Program. Eclipse IDE most recent. java 17.0.1.
program should prompt user a string representing a binary number. word must be 8 digits or less and must contain only 1s and 0s. If word is not 8 digits or less program should prompt user to make it such. If input does not contain only 1s and 0s, program should prompt user to only include 1s and 0s. I will post code that i have so far, i know that it is incomplete but im pretty much frozen where im at not sure where to go with it.
public static void main(String[] args) {
boolean isValid=false;
String bin = "";
int power =0;
int x=0;
long decimal = 0;
int answer=0;
Scanner sc = new Scanner(System.in);//create input method
do {
System.out.println("Please enter a binary number: ");//prompt user for binary number
bin = sc.nextLine();//bin equals input
if(bin.length()<=8) {//if length is 8 or less digits
for(int i =bin.length()-1; i>=0; i--) {//for the char at index i
if(((bin.charAt(i)=='1'))||(bin.charAt(i)=='0')){
isValid=true;
x=bin.indexOf(i);
power++;//power increase by 1 for each digit
decimal=(long)Math.pow(2,power);
System.out.println("The binary number you entered converted to decimal is: " + decimal);
break;
}else
isValid=false;
System.out.println("Invalid number. Please enter binary number containing only 1s and 0s:");
}
}else
System.out.println("Invalid number. Please enter binary number with 8 or less digits: ");
}
while(isValid=true);
}
}
output i get is:
Please enter a binary number:
0101
The binary number you entered converted to decimal is: 1
Please enter a binary number:
1111
The binary number you entered converted to decimal is: 2
Please enter a binary number:
1111111
The binary number you entered converted to decimal is: 4
Please enter a binary number:
111
The binary number you entered converted to decimal is: 8
Please enter a binary number:
11
The binary number you entered converted to decimal is: 16
Please enter a binary number:
01
The binary number you entered converted to decimal is: 32
Please enter a binary number:
Solution 1:[1]
I fixed the code for you.
- The result of
Math.pow()is added todecimalwhen the digit is one, and moved this line beforepower++because if the last digit is 1 it would have a value of 1 but the original code would add 2. 2 to the power of 0 is 1, so not incrementing power before the first pow is correct. - Moved
breakto theelsecondition and moved the output of the decimal number to be the last line of the loop. - Added missing curly brackets to both of the
elseclauses. Withbreakremoved, theInvalid numberis always printed. - Initialize
decimalandpowerto zero at beginning of loop. Without setting these to zero, subsequent numbers converted give the wrong output. - Only prints the result if it is valid.
- Go to next iteration with
continueif number is more than 8 digits.
public static void main(String[] args) {
boolean isValid=false;
String bin = "";
int power =0;
int x=0;
long decimal = 0;
int answer=0;
Scanner sc = new Scanner(System.in);//create input method
do {
System.out.println("Please enter a binary number: ");//prompt user for binary number
decimal = 0;
power = 0;
bin = sc.nextLine();//bin equals input
if(bin.length()<=8) {//if length is 8 or less digits
for(int i =bin.length()-1; i>=0; i--) {//for the char at index i
if(((bin.charAt(i)=='1'))||(bin.charAt(i)=='0')){
isValid=true;
x=bin.indexOf(i);
if(bin.charAt(i) == '1'){
decimal+=(long)Math.pow(2,power);
}
power++;//power increase by 1 for each digit
}else {
isValid=false;
System.out.println("Invalid number. Please enter binary number containing only 1s and 0s:");
break;
}
}
}else {
System.out.println("Invalid number. Please enter binary number with 8 or less digits: ");
continue;
}
if (isValid) {
System.out.println("The binary number you entered converted to decimal is: " + decimal);
}
}
while(isValid=true);
}
Output:
Please enter a binary number:
11000110
The binary number you entered converted to decimal is: 198
Please enter a binary number:
10100010
The binary number you entered converted to decimal is: 162
88437640
Invalid number. Please enter binary number containing only 1s and 0s:
Please enter a binary number:
123456789
Invalid number. Please enter binary number with 8 or less digits:
Please enter a binary number:
11111111 The binary number you entered converted to decimal is: 255 Please enter a binary number:
^C
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
