- #1
bchapa26
- 4
- 0
I am writing a program using JOptionPane that asks the user to input a decimal number, and then outputs the decimal number in binary. The program then asks the user if they would like to convert another decimal to enter y/n (with case being irrelevant). I was able to ask the user for the decimal and do the conversion and output the binary answer, however I do not know how to deal with the user entering characters in the JOptionPane box. I want to use something like char answer = Character.parseChar();, but all of my googling and researching has told me that this does not exist. If you could lead me in the right direction that would be great! I've been stuck on this small detail for way too long.
My code is below:
package p032;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class P032 {
public static void main(String[] args) {
String decimalToConvertString =
JOptionPane.showInputDialog("Enter decimal to convert");
int decimalToConvert = Integer.parseInt(decimalToConvertString);
String binaryNumberString = "";
while (decimalToConvert != 0){
int remainder = decimalToConvert % 2;
String remainderString = String.valueOf(remainder);
binaryNumberString = remainderString + binaryNumberString;
decimalToConvert = (decimalToConvert - remainder) / 2;
}
JOptionPane.showMessageDialog(null, "Binary number: " + binaryNumberString);
String answerString = JOptionPane.showInputDialog("Convert another (y/n)?");
My code is below:
package p032;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class P032 {
public static void main(String[] args) {
String decimalToConvertString =
JOptionPane.showInputDialog("Enter decimal to convert");
int decimalToConvert = Integer.parseInt(decimalToConvertString);
String binaryNumberString = "";
while (decimalToConvert != 0){
int remainder = decimalToConvert % 2;
String remainderString = String.valueOf(remainder);
binaryNumberString = remainderString + binaryNumberString;
decimalToConvert = (decimalToConvert - remainder) / 2;
}
JOptionPane.showMessageDialog(null, "Binary number: " + binaryNumberString);
String answerString = JOptionPane.showInputDialog("Convert another (y/n)?");