简体   繁体   中英

I want my graphical user interface to accept an integer

Hello I'm having trouble with my work. I want a graphical user interface to accpet a single statement in the form "insert name number" .

For example of such a command is " insert Whiz 105 "

I've programed Java to use "insert" word in the user input and also string. However I don't know how to make Java detect both string and integer in one statement.

How do I create a statement that allows me to separate String and Integers, when user enters both them in the GUI?. For example instead of creating a two inputs for String and Integers, I use one input instead to refer to both of them.

Here is my code, please base your answers to it.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.*;

/**
   This class is used to demonstrate
   the operations in the LinkedList1 class.
*/

public class LinkedList1Demo extends JFrame
{    
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private LinkedList<String> names;


    private JTextArea  listView;
    private JTextField cmdTextField;
    private JTextField resultTextField;

    public LinkedList1Demo()
    {
       names = new LinkedList<String>(); 
       listView = new JTextArea();
       cmdTextField = new JTextField();
       resultTextField = new JTextField();       

       // Create a panel and label for result field
       JPanel resultPanel = new JPanel(new GridLayout(1,2));
       resultPanel.add(new JLabel("Command Result"));
       resultPanel.add(resultTextField);
       resultTextField.setEditable(false);
       add(resultPanel, BorderLayout.PAGE_START);

       // Put the textArea in the center of the frame
       add(listView);
       listView.setEditable(false);
       listView.setBackground(Color.WHITE);
       listView.setPreferredSize(new Dimension(200, 25));



       // Create a panel and label for the command text field
       JPanel cmdPanel = new JPanel(new GridLayout(1,2));
       cmdPanel.add(new JLabel("Command Name:"));
       cmdPanel.add(cmdTextField);
       add(cmdPanel, BorderLayout.PAGE_END);  
       cmdTextField.addActionListener(new CmdTextListener());
       cmdPanel.setPreferredSize(new Dimension(200, 25));

       // Set up the frame
       setTitle("Linked List Demo");
       setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       pack();
       setVisible(true);       
    }

    /**
       Private class that responds to the command that 
       the user types into the command entry text field.
    */

    private class CmdTextListener
            implements ActionListener
    {            
        public void actionPerformed(ActionEvent evt)
        {
            String cmdText = cmdTextField.getText();

            Scanner sc = new Scanner(cmdText);

            String cmd = sc.next();

            if (cmd.equals("insert"))
            {
                if (sc.hasNextInt())
                {
                    // add index element
                    int index = sc.nextInt();
                    String element = sc.next();
                    names.add(index, element);                
                }
                else
                {  
                    // add element
                    String element = sc.next();
                    names.add(element);                
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("remove"))
            {
                if (sc.hasNextInt())
                {
                    // remove index
                    int index = sc.nextInt();
                    Object obj = names.remove(index);
                    String res = obj.toString();
                    resultTextField.setText(res);              
                }
                else
                {
                    // remove element
                    String element = sc.next();
                    boolean res = names.remove(element);
                          String resText = String.valueOf(res);
                    resultTextField.setText(resText);
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("isempty"))
            {
                    String resText = String.valueOf(names.isEmpty());
                resultTextField.setText(resText);
                return;
            }
            if (cmd.equals("size"))
            {
                   String resText = String.valueOf(names.size());
               resultTextField.setText(resText);
               return;
            }

        }
    }

    /**
       The main method creates an instance of the 
       LinkedList1Demo class which causes it to 
       display its window.
    */

    public static void main(String [ ] args)
    {
        new LinkedList1Demo();

    }

}

I'd ask you to explain your requirement better. But a quick idea if you're sure you'll only get a bunch of strings and one integer, would be to separate the string by using a tokenizer or, preferably, the split method.

Then you iterate through each individual word and try with the "Integer.parseInt()" method. If a NumberFormatException raises, it's a string and you should append it to your string object. Otherwise, it's an integer and you can threat it as so.

Use the String method split to split the String.

String input = /* pull from the text box */.
String[] tokens = input.split("\\s");
if (tokens.length == 3)
{
     String command = tokens[0];
     String intValue= tokens[2];
}

You can try something like:

String cmd = sc.next();

try {

    int number = Integer.parseInt(cmd);

} catch (NumberFormatException e) {
     if (cmd.equals("insert"))
            {
                if (sc.hasNextInt())
                {
                    // add index element
                    int index = sc.nextInt();
                    String element = sc.next();
                    names.add(index, element);                
                }
                else
                {  
                    // add element
                    String element = sc.next();
                    names.add(element);                
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("remove"))
            {
                if (sc.hasNextInt())
                {
                    // remove index
                    int index = sc.nextInt();
                    Object obj = names.remove(index);
                    String res = obj.toString();
                    resultTextField.setText(res);              
                }
                else
                {
                    // remove element
                    String element = sc.next();
                    boolean res = names.remove(element);
                          String resText = String.valueOf(res);
                    resultTextField.setText(resText);
                }
                listView.setText(names.toString());
                pack();
                return;
            }
            if (cmd.equals("isempty"))
            {
                    String resText = String.valueOf(names.isEmpty());
                resultTextField.setText(resText);
                return;
            }
            if (cmd.equals("size"))
            {
                   String resText = String.valueOf(names.size());
               resultTextField.setText(resText);
               return;
            }
}

This will attempt to parse the input to an integer. If it is a number then it will succeed otherwise it will fail and enter your catch block and be processed as a string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM