I am writing an application for class. The user is prompted to enter a number, then select what to do what that number. I'm using JPanel and JOptionpane for input and console for output.

The application is supposed to: prompt for input, ask for what to do with the input, display results, prompt for input again.

I have everything the way I want it, except I cannot get the input prompt to loop correctly. Right now when you compile the code your prompted for the input, then you can select what to do with it as many times as you like before closing the application. I'm not sure how to exit the JPanel and ask for another user input.

Here is my code so far:

package phase4individualproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Weylin N. Bowman.
 */
public class project4 {
    
    public static void main(String[] args) {


        JFrame Math = new JFrame("Calculator");
        Math.setSize(300, 150);
        Math.setLocation(200,200);
        Math.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent w) {System.exit(0); }
        });

        JPanel options = new JPanel();
        final ButtonGroup mathOptions = new ButtonGroup();
        JRadioButton radioButton;
        options.add(radioButton = new JRadioButton("Square"));
        radioButton.setActionCommand("1");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Cube"));
        radioButton.setActionCommand("2");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Double"));
        radioButton.setActionCommand("3");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Halve"));
        radioButton.setActionCommand("4");
        mathOptions.add(radioButton);

        JPanel submit = new JPanel();
        JButton calc = new JButton("Calculate");
        submit.add(calc);

        Container content = Math.getContentPane();
        content.setLayout(new GridLayout(3, 1));
        content.add(options);
        content.add(submit);

            String number;
            int close = 0;

           do{
            number = JOptionPane.showInputDialog("Please enter any number.");
            final int num = Integer.parseInt(number);

        calc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.out.println("You entered: " + num);

                String selected = mathOptions.getSelection().getActionCommand();
                int selectedNum = Integer.parseInt(selected);

                if(selectedNum == 1){
                    System.out.println(num * num);
                }
                else if(selectedNum == 2){
                    System.out.println(num * num * num);
                }
                else if(selectedNum == 3){
                    System.out.println(num * 2);
                }
                else if(selectedNum == 4){
                    System.out.println(num/2);
                    System.exit(1);
                }
                else{
                    System.exit(1);
                }
            }
        });

Math.setVisible(true);

    }while(false);
           }
}

I was thinking about simply adding an input box onto the JPanel Menu. That way you could type a number, select a radio button then hit submit and that same window would stay open. I couldn't figure out how to do that though. I kept getting incompatible type errors.

I was thinking about simply adding an input box onto the JPanel Menu. That way you could type a number, select a radio button then hit submit and that same window would stay open. I couldn't figure out how to do that though. I kept getting incompatible type errors.

Post your best shot at doing this and I'll help. P.S. your code you already posted doesn't make sense; why would you say do... while(false)? That's only going to do something once, and if you want to do it once, you don't need a loop. You should be looping while the user doesn't want to quit, so something like

do{
String s = JOptionPane.someInputPaneHere();
} while (!s.equals("QUIT"));

I was able to find online how to add an input box JTextField to my JPanel. This seems to work fine so I commented out my JOptionPane window.

The dowhile loop above was just there for testing, I was trying many different things to get this to loop properly. Originally it was jsut set to 'true', but I changed it to false to see what would happen. I have removed that, as I don't think I need to loop anymore with JPanel since it has it's own closing method when the user decides to do so.

However, I am no longer able to parse the string for input into text. When I compile the application this is what I receive below:


run:
Exception in thread "main" java.lang.NumberFormatException: For input string: ""
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
at java.lang.Integer.parseInt(Integer.java:470)
at java.lang.Integer.parseInt(Integer.java:499)
at phase4individualproject.project4.main(project4.java:56)
Java Result: 1
BUILD SUCCESSFUL (total time: 1 second)

Compiling the code without the parseInt makes the GUI work fine, but then of course nothing else does.

package phase4individualproject;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

/**
 *
 * @author Weylin N. Bowman.
 */
public class project4 {

    public static void main(String[] args) {

        JFrame Math = new JFrame("Calculator");
        Math.setSize(300, 150);
        Math.setLocation(200,200);
        Math.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent w) {System.exit(0); }
        });

        JPanel textField = new JPanel();
        JTextField input = new JTextField("", 20);
        textField.add(input);

        JPanel options = new JPanel();
        final ButtonGroup mathOptions = new ButtonGroup();
        JRadioButton radioButton;
        options.add(radioButton = new JRadioButton("Square"));
        radioButton.setActionCommand("1");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Cube"));
        radioButton.setActionCommand("2");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Double"));
        radioButton.setActionCommand("3");
        mathOptions.add(radioButton);
        options.add(radioButton = new JRadioButton("Halve"));
        radioButton.setActionCommand("4");
        mathOptions.add(radioButton);

        JPanel submit = new JPanel();
        JButton calc = new JButton("Calculate");
        submit.add(calc);

        Container content = Math.getContentPane();
        content.setLayout(new GridLayout(3, 1));
        content.add(textField);
        content.add(options);
        content.add(submit);

           final String number;
            int close = 0;

           /* number = JOptionPane.showInputDialog("Please enter any number.");*/
            number = input.getText();
            final int num = Integer.parseInt(number);

        calc.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {

                System.out.println("You entered: " + num);

                String selected = mathOptions.getSelection().getActionCommand();
                int selectedNum = Integer.parseInt(selected);

                if(selectedNum == 1){
                    System.out.println(num * num);
                }
                else if(selectedNum == 2){
                    System.out.println(num * num * num);
                }
                else if(selectedNum == 3){
                    System.out.println(num * 2);
                }
                else if(selectedNum == 4){
                    System.out.println(num/2);
                    System.exit(1);
                }
                else{
                    System.exit(1);
                }
            }
        });

Math.setVisible(true);
           }
}

"I am no longer able to parse the string for input into text..."

Huh? The JTextField class has a getText method. If you call myJTextField.getText() it will return a String. "Text" and "a String" mean the same thing.. did you mean you want to parse a String into an Integer? For that you could use Integer.parseInt(yourStringHere);

Sorry, my verbiage is a little off with all this stuff. Exactly. I want to parse the text from the JTextField into an integer. An I am doing that here:

number = input.getText();
            final int num = Integer.parseInt(number);

However, when running the project it fails with the error above.

why wouldnt you put the whole body of the main method inside a while (true) { } loop so that it infinitely loops until you hit submit with nothing selected or some such thing. I believe the technical term for what i am trying to say is make it a loop-and-a-half.

TJ

Does not seem to work. Keeps throwing a 1 line error: "while loop expected".

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.