这是我的字符串数组:
String[] selection = { "0 + 0", "0.88 + 0.21", "-0.21 + 0.77", "-1.23 + 0.03" };
然后我创建了一个JComboBox:
JComboBox<String> jcb = new JComboBox<String>(selection);
jcb.addActionListener(new ComboListener());
这是ActionListener:
public class ComboListener implements ActionListener {
public void actionPerformed(ActionEvent e) {
JComboBox<String> cb = (JComboBox<String>) e.getSource();
String selection = (String) cb.getSelectedItem();
String[] parts = selection.split(" + ");
System.out.println(parts[0]);
System.out.println(parts[1]);
}
}
这是我选择一个选项时打印出的内容,比如0.88 + 0.21:
0.88 + 0.21
错误:
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 1
at GUI$ComboListener.actionPerformed(GUI.java:140)
at javax.swing.JComboBox.fireActionEvent(Unknown Source)
at javax.swing.JComboBox.setSelectedItem(Unknown Source)
at javax.swing.JComboBox.setSelectedIndex(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$Handler.mouseReleased(Unknown Source)
at java.awt.AWTEventMulticaster.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at javax.swing.plaf.basic.BasicComboPopup$1.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
任何人都知道我哪里出错了?我拆分字符串的逻辑看起来很好。非常感谢;
答案 0 :(得分:5)
split
将正则表达式作为参数。但+
是一个特殊的正则表达式字符。所以你需要逃避它:
String[] parts = selection.split(" \\+ ");
另一种解决方案是使用Pattern.quote
:
String[] parts = selection.split(Pattern.quote(" + "));
String[] selection = {"0 + 0", "0.88 + 0.21", "-0.21 + 0.77", "-1.23 + 0.03"};
for(String s : selection){
System.out.println(Arrays.toString(s.split(" \\+ ")));
}
成功打印:
[0, 0]
[0.88, 0.21]
[-0.21, 0.77]
[-1.23, 0.03]
答案 1 :(得分:0)
正如异常所指出的那样,分裂并不像你想象的那样(想到它):
selection.split(" + ");
问题是split需要使用正则表达式,因此您需要转义+
selection.split(" \\+ ");