user2296424
user2296424

Reputation:

Combobox Selected Item

Here is my array of Strings :

String[] selection = { "0 + 0", "0.88 + 0.21", "-0.21 + 0.77", "-1.23 + 0.03" };

I then create a JComboBox:

    JComboBox<String> jcb = new JComboBox<String>(selection);
    jcb.addActionListener(new ComboListener());

Here is the 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]);
    }
}

Here's what is printed out when I select an option, say 0.88 + 0.21:

0.88 + 0.21

With the error:

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)

Anyone got any idea where I'm going wrong? My logic of splitting up the string seems to be fine. Many thanks;

Upvotes: 0

Views: 327

Answers (2)

Alexis C.
Alexis C.

Reputation: 93842

split takes a regular expression as parameter. But + is a special regexp character. So you need to escape it:

String[] parts = selection.split(" \\+ ");

Another solution would be to use 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(" \\+ ")));
}

Successfully prints:

[0, 0]
[0.88, 0.21]
[-0.21, 0.77]
[-1.23, 0.03]

Upvotes: 5

Mike Dinescu
Mike Dinescu

Reputation: 55720

As the exception points out, the split doesn't work the way you intended (thought it would):

selection.split(" + ");

The problem is that split takes a regular expression so you need to escape the +

selection.split(" \\+ ");

Upvotes: 0

Related Questions