Werte aus einem Textfeld lesen

mithilfe von Textfeld.getText(); bekommst du den Text diesen kannst du in einem String speichern und anschließend mithilfe von split(+); kannst du den String beim + Teilen und addieren.

Es scheint allerdings als würdest du einen Taschenrechner programmieren wollen, falls ja, solltest du dir deine Vorgehensweise nochmal überlegen.
 
Ich hab das jetzt so gemacht, aber da kommen nur ganz viele Errors:

Java:
			String[] temp;
			String erg = textField1.getText();
			String plus = "+";
			temp = erg.split(plus);
			textField2.setEditable(false);
			textField3.setText(""+temp[0]+temp[1]);
 
Für Methoden braucht man keine Imports.

Ich gebe dir ein Tipp: Wenn du schreibst, dass da viele Errors kommen, dann poste doch mal die Errors, oder sollen wir hier rumraten?
 
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 MyGUI.Auflösen(MyGUI.java:177)
	at MyGUI$3.actionPerformed(MyGUI.java:79)
	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.dispatchEventImpl(Unknown Source)
	at java.awt.EventQueue.access$000(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.awt.EventQueue$1.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.awt.EventQueue$2.run(Unknown Source)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$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)
 
Nun ja, String.split() erwartet eine Regual Expression (String (Java Platform SE 6)).

Wenn du dich noch nicht mit Regex auskennst lass dir hier sagen dass sonderzeichen wie das '+' encoded werden muss. Versuchs mal mit .split("\\+").

Ausserdem musst du die beiden Argumente die du erhaelst in Zahlen (Integer) umwandeln ansonsten 'addierst' du 2 Zeichenketten, was wohl nicht in deinem Sinne ist.
 
Das ist nur EIN Fehler.

Kann man in Google eingeben.

Split erwartet RegEx und das "+" ist ein RegEx Keword und muss escaped werden "\"
 

Neue Themen


Zurück
Oben