Was ist an dieser einfachen funktion falsch?

StrikeTom

Bekanntes Mitglied
Hallo Leute,
Was ist an dieser einfachen funktion falsch?
Java:
public int calculate(String aufgabe)
	{
		if(aufgabe.contains("+"))
		{
			String[] zahlen = aufgabe.split("+");
			int z1 = Integer.parseInt(zahlen[0]);
			int z2 = Integer.parseInt(zahlen[1]);
			return z1 + z2;
		}
		return 0;
	}
Danke im voraus🙂
 
Fehler:
Code:
Exception in thread "AWT-EventQueue-0" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
	at java.util.regex.Pattern.error(Unknown Source)
	at java.util.regex.Pattern.sequence(Unknown Source)
	at java.util.regex.Pattern.expr(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.util.regex.Pattern.<init>(Unknown Source)
	at java.util.regex.Pattern.compile(Unknown Source)
	at java.lang.String.split(Unknown Source)
	at java.lang.String.split(Unknown Source)
	at SwingMathe.Rechner.calculate(Rechner.java:25)
	at SwingMathe.Rechner.actionPerformed(Rechner.java:37)
	at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
	at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
	at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
	at java.awt.Component.processMouseEvent(Unknown Source)
	at javax.swing.JComponent.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.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)
Danke🙂
 
Java:
	public int calculate(String aufgabe)
    {
        if(aufgabe.contains("+"))
        {

            String[] zahlen= aufgabe.split("\\+");
            int z1 = Integer.parseInt(zahlen[0]);
            int z2 = Integer.parseInt(zahlen[1]);
            return z1 + z2;
        }
        return 0;
    }

Reguläre Ausdrücke, die in der RegExp für andere "Ausdrücke" stehen (+, -, * usw), müssen mit einem Doppelbackslash maskiert werden. Ein einfacher Backslash langt nicht, da ansonsten ein Steuerungszeichen erwartet wird!
 

Zurück
Oben