Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

1. ## JAVA problem

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!!

2. In the future please select a better title that's more descriptive of the problem.

This tutorial describes the usage of JOptionPane. You'll need to write a gui to run the program.

3. Is this what you are looking for. I took the code of yours for calculating decimal values but made soem cahnges. I would suggest to not to just copy the code, but use this an example and build more around this..

import javax.swing.JOptionPane;

public class JOptionPaneTest1 {

public static void main(String[] args) {
String ans;
ans = JOptionPane.showInputDialog(null, "Enter Binary Value");
JOptionPane.showMessageDialog(null, "Decimal Value = " + answer);

System.exit(0);
}

public int convert(String text){

//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 = text.toCharArray();
int j=binaryArray.length-1;
// 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 = 0; i<binaryArray.length; 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, j);
}
j--;
}

return decimalNumber;
}

}

4. Originally Posted by spchinta
Is this what you are looking for. I took the code of yours for calculating decimal values but made soem cahnges. I would suggest to not to just copy the code, but use this an example and build more around this..

import javax.swing.JOptionPane;

public class JOptionPaneTest1 {

public static void main(String[] args) {
String ans;
ans = JOptionPane.showInputDialog(null, "Enter Binary Value");
JOptionPane.showMessageDialog(null, "Decimal Value = " + answer);

System.exit(0);
}

public int convert(String text){

//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 = text.toCharArray();
int j=binaryArray.length-1;
// 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 = 0; i<binaryArray.length; 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, j);
}
j--;
}

return decimalNumber;
}

}
In the future, please ensure your code is wrapped in [code][/code] or [php][/php] tags to preserve formatting.

As this is a homework assignment, you should not be providing directly usable code for your replies; instead limit it to minor corrections or little implementation (or other tutorials for examples). Providing full use code does not allow new programmers to teach themselves.

5. I am new to this forum and using this to refersh my coding skills.
Thanks for the suggestion.I was wondering how i can wrap code the way others do. I saw this earlier but missed it..

Also, about the code, I made corrections to the convert method and gave an eaxmple of JOptionsPane. She/he still has to add logic for error messages.

I agree, In some other posts too, I gave all code. That's my bad habbit to explain everything in clear. Giving examples rather than providing code is a good idea too. I will take care of that next time..

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•