I am just doing some school work here and i am tasked with changing a binary into a decimal using the input dialog box in JOptionPane.
What i basically need to accomplish is being able to type in a binary and get a message box to pop up with the decimal and if the input is invalid an error message comes up.
I have found many ways to get an actual print out of the decimal but i can't figure out how to link the dialog box to the actual calculations.
here is what i have so far
Code:
import java.lang.Math;
public class BinaryToDecimal
{
public static void main(String args[])
{
//declare the original String object
String binaryString = "10011110";
//declare the char array
char[] binaryArray;
int decimalNumber = 0;
//convert string into array using toCharArray() method of string class
binaryArray = binaryString.toCharArray();
// this for loop may be different from others you've seen. instead of starting at 0 and counting up, it's going to start at the max and count down to zero
for (int i = binaryArray.length - 1; i >= 0; i--)
{
// Now i is going to be the power, and binaryArray[i] will be the 1 or 0 value
// So if binaryArray[i] = 1, then add 2 to the power of i to the decimalNumber
if (binaryArray[i] == "1")
{
decimalNumber += Math.pow(2, i);
}
}
//
}
}
Any help on this is appreciated!!