Beginner needs help with simple java assignment.
I'm trying to implement a program that reads binary numbers from the user and returns the equivalent in the normal (decimal) numeral system.
This is what I've come up with so far:
Code :
import java.util.Scanner;
public class BinaryConverter {
public static void main(String[] args) {
int binary;
Scanner scan = new Scanner (System.in);
System.out.print("Enter a binary number (0 to quit): ");
binary = scan.nextInt();
String binaryString = Integer.toString(binary);
while (binary != 0)
System.out.println("The binary number " +binaryString +" corresponds to " +toDecimal(binaryString)+" in the decimal numeral system.");
}
public static double toDecimal(String binaryString)
{
double decValue = 0;
int i = 0;
for(i=0; i < binaryString.length(); i++)
if (binaryString.charAt(binaryString.length() - i) == 1){
decValue = Math.pow(2, binaryString.length()- i );
}
return decValue;
}
My program fails to carry out this task.
What can I do to ensure that the variable decValue summarizes the result from "decValue = Math.pow(2, binaryString.length()- i " each and every time the for-loop is runned through and invokes that code?
I also get a string index out of bound error, but I can't see where the program errs.
Any help is appreciated.
Re: Beginner needs help with simple java assignment.
You reset the decValue every time though the loop, rather than incrementing it:
Code :
decValue [COLOR="Red"]+=[/COLOR] Math.pow(2, binaryString.length()- i );
Also, inspect your loops closely. You are getting exceptions because your loop is written in such a way as to try and access an array element that is not within its bounds.
Re: Beginner needs help with simple java assignment.
Ok, I've made som necessary changes to the program.
Code:
Code :
import java.util.Scanner;
public class BinaryConverter {
public static void main(String[] args) {
int binary = 2;
Scanner scan = new Scanner (System.in);
String binaryString = Integer.toString(binary);
while (binary != 0){
System.out.print("Enter a binary number (0 to quit): ");
binary = scan.nextInt();
System.out.println("The binary number " +binary +" corresponds to: " +toDecimal(binaryString));
}
}
public static double toDecimal(String binaryString) {
double decValue = 0;
for (int i=0; i < binaryString.length(); i++)
if (binaryString.charAt(i) == '1')
decValue += Math.pow(2, binaryString.length() - 1 - i);
return decValue;
}
}
I have two problems, firstly, the program will just return 0.0 from any input. Secondly, the program doesn't quit when the user enters zero. The latter is a minor problem, but advice on both problems are highly appreciated.
Re: Beginner needs help with simple java assignment.
Instead of using , use
Code :
binaryString = scan.next()
. This will get you the necessary binaryString to convert to decimal. It's also unnecessary to have the binary variable. toDecimal is already returning the correct decimal value, so print this out:
Code :
System.out.println("The binary number " + binaryString + " corresponds to: " + toDecimal(binaryString));
To fix the check in the while loop, all you need to change it to is this:
Code :
while (toDecimal(binaryString) != 0)
I would also change the return type of toDecimal to a either int or long because there's no need to handle binary decimal numbers (floating point number conversion is a whole new can of worms :( ). This isn't really necessary, but something that I would recommend (choose your variable types to fit the purpose).