Java JOptionPane and accepting characters as input

  • Comp Sci
  • Thread starter bchapa26
  • Start date
  • Tags
    Input Java
In summary, the user enters a decimal number in JOptionPane, and the program converts it to binary. The user can then enter another decimal number and the program will convert it to binary again.
  • #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)?");
 
Physics news on Phys.org
  • #2
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.
 
  • #3
Since you are converting base-10 numbers, the user should not be entering characters. If this is something you need to guard against, read the user's input as a string, and then convert the string to an int. Here's a link to some code that is similar: http://download.oracle.com/javase/tutorial/java/data/converting.html
 

Related to Java JOptionPane and accepting characters as input

1. How do I use JOptionPane to accept user input in Java?

JOptionPane is a Java class that allows you to create dialog boxes and display messages in your program. To use it for accepting user input, you can use the showInputDialog() method. This method takes in a String message as input and returns the user's input as a String.

2. Can JOptionPane accept characters as input?

Yes, JOptionPane can accept characters as input. When using the showInputDialog() method, the user's input will be returned as a String, but you can convert it to a character using the charAt() method.

3. How do I validate the user's input in JOptionPane?

To validate the user's input in JOptionPane, you can use conditional statements or regular expressions. Conditional statements allow you to check if the input meets certain criteria, while regular expressions allow you to match patterns in the input.

4. What happens if the user clicks the cancel button in the JOptionPane dialog box?

If the user clicks the cancel button in the JOptionPane dialog box, the showInputDialog() method will return null. Therefore, it is important to handle this case in your code to prevent any errors.

5. Is there a way to customize the appearance of the JOptionPane dialog box?

Yes, you can customize the appearance of the JOptionPane dialog box by using the setIcon() and setOptionType() methods. The setIcon() method allows you to add an icon to the dialog box, while the setOptionType() method allows you to change the buttons displayed in the dialog box.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
7K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
8K
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
11K
Back
Top