你好,我的工作遇到了麻烦。我想要一个图形用户界面,以接受形式的单个语句"insert name number"。
例如:"insert Whiz 105"
我已经编程Java在用户输入和字符串中使用"插入"字。然而,我不知道如何使Java在一个语句中检测字符串和整数。
当用户在GUI中输入字符串和整数时,如何创建一个允许我分离字符串和整数的语句?例如,我没有为String和Integers创建两个输入,而是使用一个输入来引用它们。
这是我的代码,请根据它的答案。
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();
}
}
使用String
方法split
拆分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];
}
我希望你能更好地解释你的需求。但是,如果你确定你只会得到一堆字符串和一个整数,一个快速的想法是使用标记器或最好是split方法来分隔字符串。
然后遍历每个单独的单词,并尝试使用"Integer.parseInt()"方法。如果引发NumberFormatException,则它是一个字符串,您应该将其附加到字符串对象。否则,它就是一个整数,你可以这样威胁它
您可以尝试这样做:
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;
}
}
这将尝试将输入解析为整数。如果它是一个数字,那么它将成功,否则它将失败并进入catch块并作为字符串处理。