Java JOptionPane and accepting characters as input

  • Context: Comp Sci 
  • Thread starter Thread starter bchapa26
  • Start date Start date
  • Tags Tags
    Input Java
Click For Summary
SUMMARY

This discussion focuses on using Java's JOptionPane for user input and handling character input when converting decimal numbers to binary. The user successfully implemented the conversion but faced challenges with character input validation. The solution involves reading user input as a string and validating it before conversion to an integer, as Java does not support direct character parsing from strings. The provided code snippet demonstrates the conversion process and highlights the need for proper input handling.

PREREQUISITES
  • Understanding of Java programming language
  • Familiarity with JOptionPane for user input
  • Knowledge of integer and string data types in Java
  • Basic concepts of binary number conversion
NEXT STEPS
  • Implement input validation for JOptionPane using regular expressions
  • Explore Java exception handling to manage input errors
  • Learn about Java's Scanner class for console input
  • Research binary number operations in Java for further optimization
USEFUL FOR

Java developers, software engineers, and anyone interested in improving user input handling and data validation in GUI applications using JOptionPane.

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)?");
 
Physics news 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.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
18K
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
8K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K