Java JOptionPane and accepting characters as input

  • Context: Comp Sci 
  • Thread starter Thread starter bchapa26
  • Start date Start date
  • Tags Tags
    Input Java
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 13K views
bchapa26
Messages
4
Reaction score
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)?");
 
on Phys.org
When you post code for us to look at, put a [ code ] tag at the beginning and a [ /code ] tag at the end (but without the extra spaces. Doing so preserves your indentation and makes the code easier to read. I have done this for your example.
bchapa26 said:
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:
Code:
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)?");

First off, you're missing two closing braces at the end.