Reputation: 1015
I am a novice programmer trying to write a program that converts an entered Binary number into a decimal number. As far as I can tell, the math and code are right, and no compilation errors are returned, however the number that is output is NOT the correct decimal number. My code is as follows:
String num;
double result = 0;
do {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a binary number or enter 'quit' to quit: ");
num = in.nextLine();
int l = num.length();
if (num.indexOf("0")==-1 || num.indexOf("1")==-1 ){
System.out.println("You did not enter a binary number.");
}
for (int i = 0; i < l; i++)
{
result = result + (num.charAt(i) * Math.pow(2, (l - i)));
}
System.out.println("The resulting decimal number is: " +result);
} while (!num.equals("quit"));
if (num.equals("quit")){
System.out.println("You chose to exit the program.");
return;
}
Any Help you could give would be much appreciated. I tried making my question as clear as possible but if you have any questions I will try to answer the best I can. I have not been doing this long. All I need is for someone to look it over and hopefully find the error I made somewhere, thanks.
Upvotes: 0
Views: 2370
Reputation: 2639
Integer.parseInt(string, base)
parse string into an integer using "base" radix, if it cannot be converted, it raises an exception.
import java.util.Scanner;
public class Convertion {
public static void main(String[] args) {
String num;
Scanner in = new Scanner(System.in);
System.out.println("Please enter a binary number");
num = in.nextLine();
try{
//this does the conversion
System.out.println(Integer.parseInt(num, 2));
} catch (NumberFormatException e){
System.out.println("Number entered is not binary");
}
}
}
Upvotes: 0
Reputation: 129587
Change
result = result + (num.charAt(i) * Math.pow(2, (l - i)));
to
result = result + ((num.charAt(i) - '0') * Math.pow(2, i));
or more compactly,
result += (num.charAt(i) - '0') * Math.pow(2, i);
Remember that the character '0'
is not the same thing as the number 0
(same for '1'
and 1
); num.charAt(i)
returns the character not the integer.
int a = '0';
int b = 0;
System.out.println(Math.pow(2, a));
System.out.println(Math.pow(2, b));
Output:
2.81474976710656E14
1.0
Big difference isn't there?
Upvotes: 2
Reputation: 1054
The function String.charAt();
does not return the number 0 or 1 which you can multiply with the bit but the character "id". You need to convert the String / char to a number.
String num;
double result = 0;
do {
Scanner in = new Scanner(System.in);
System.out.println("Please enter a binary number or enter 'quit' to quit: ");
num = in.nextLine();
int l = num.length();
if (num.indexOf("0")==-1 || num.indexOf("1")==-1 ){
System.out.println("You did not enter a binary number.");
}
for (int i = 0; i < l; i++)
{
result = result + (Integer.parseInt(num.substring(i,i+1)) * Math.pow(2, (l - i)));
}
System.out.println("The resulting decimal number is: " +result);
} while (!num.equals("quit"));
if (num.equals("quit")){
System.out.println("You chose to exit the program.");
return;
}
By the way: Why is a string that does not contain either a 0 or a 1 not a binary numer? Think of 1111
for example. I think you should better check for "neither 0 nor 1"
if (num.indexOf("0")==-1 && num.indexOf("1")==-1 ){
System.out.println("You did not enter a binary number.");
}
Upvotes: 1
Reputation: 83577
Note that num.charAt(i)
gives the ASCII code for the character at position i
. This is not the value you want. You need to convert each character digit to an int
before you do any math with the value.
Upvotes: 0