Get value from text fields?

  • (2 Pages)
  • +
  • 1
  • 2

24 Replies - 13243 Views - Last Post: 07 December 2010 - 05:03 PM Rate Topic: -----

#1 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Get value from text fields?

Posted 20 November 2010 - 04:41 PM

I have this assignment where in the values of two text fields are supposed to swap if the first value is higher than the other, but I don't know how to do that.

This is the code I have:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithTextFields extends Applet implements ActionListener {
	public void init() {
		Label label1 = new Label("First text field: ");

		textField1 = new TextField("40");

		swapEm = new Button("Swap");
		swapEm.addActionListener(this);

		clear = new Button("Clear");
		clear.addActionListener(this);

		calc = new Button("Calculate");
		calc.addActionListener(this);

		Label label2 = new Label("Second text field: ");

		textField2 = new TextField("20");
		textField2.setEditable(false);

		add(label1);
		add(textField1);
		add(swapEm);
		add(label2);
		add(textField2);
		add(clear);
		add(calc);
	}

	public void actionPerformed(ActionEvent e) {

		if (textField1 > textField2) {
		String temp = textField1.getText();
		textField1.setText(textField2.getText());
		textField2.setText(temp);
		}

		if (e.getSource() == clear) {
		textField1.setText("");
		textField2.setText("");
		}

		if (e.getSource() == calc) {


		}
	}

	TextField textField1, textField2;
	Button swapEm, clear, calc;
}


This post has been edited by mchin131: 20 November 2010 - 04:58 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Get value from text fields?

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from text fields?

Posted 20 November 2010 - 04:47 PM

if (textField1 > textField2) {

How can a JTextField be compared to another JTextField ?
at least
if(textField1.getText().compareTo(textField2.getText()) < 0)
Was This Post Helpful? 1
  • +
  • -

#3 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 20 November 2010 - 04:57 PM

Thanks, but is there a way to compare the values if the first textfield to the second one? For example, if the value of the first textfield is 40 and the second is 45, then they wouldnt swap?
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from text fields?

Posted 20 November 2010 - 05:03 PM

double value1 = Double.parseDouble(textField1.getText());
double value2 = Double.parseDouble(textField2.getText());

now you can compare value1 and value2
Was This Post Helpful? 1
  • +
  • -

#5 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Get value from text fields?

Posted 20 November 2010 - 05:08 PM

The problem with this is, that the getText()-Method from the JTextField returns a String. If you want to compare this values in a mathematically way, you need to parse them to a int-Variable:
int your_int = Intenger.parseInt( textfield.getText() );


After this, you can use a normal if-statement to check for which value is higher or something.
This will only work if the user-input is a number that can be parsed to an intenger!

Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#6 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 20 November 2010 - 05:38 PM

Thanks for the help! I have another problem. I'm supposed to have the calc method perform operations based on the operator. For example, if the operator is +, add operand1 and operand2, if operator is -, subtract operand1 and operand2, etc.

My program went wrong, I'm not sure what kind of error I have.

This is what I changed of my program:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithTextFields extends Applet implements ActionListener {
	public void init() {
		Label label1 = new Label("First text field: ");

		textField1 = new TextField("");

		swapEm = new Button("Swap");
		swapEm.addActionListener(this);

		clear = new Button("Clear");
		clear.addActionListener(this);

		calc = new Button("Calculate");
		calc.addActionListener(this);

		Label label2 = new Label("Second text field: ");

		textField2 = new TextField("20");
		textField2.setEditable(false);

		operand1 = new TextField("");
		operator = new TextField("+");
		operand2 = new TextField("");

		Label resultLabel = new Label("Result: ");
		resultField = new TextField("");
		resultField.setEditable(false);

		double value1 = Double.parseDouble(textField1.getText());
		double value2 = Double.parseDouble(textField2.getText());

		int intOper1 = Integer.parseInt(operand1.getText());
		int intOper2 = Integer.parseInt(operand2.getText());


		add(label1);
		add(textField1);
		add(swapEm);
		add(label2);
		add(textField2);
		add(clear);
		add(operand1);
		add(operator);
		add(operand2);
		add(resultLabel);
		add(resultField);
		add(calc);
	}

	public void actionPerformed(ActionEvent e) {

		if(value1 > value2) {
		String temp = textField1.getText();
		textField1.setText(textField2.getText());
		textField2.setText(temp);
		}

		if (e.getSource() == clear) {
		textField1.setText("");
		textField2.setText("");
		}

		if (e.getSource() == calc) {
			if (operator.getText() == "+") {
			totalresult = intOper1 + intOper2;
			}
		System.out.println(totalresult);
		}
		repaint();
	}

	int intOper1, intOper2, totalresult;
	double value1, value2;
	TextField textField1, textField2, operand1, operand2, operator, resultField;
	Button swapEm, clear, calc;
}


Was This Post Helpful? 0
  • +
  • -

#7 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Get value from text fields?

Posted 20 November 2010 - 06:24 PM

When you check 2 String Objects for equality in java, don't use the '==' operator like you use it here:
 if (operator.getText() == "+"){...}

use the equals(String s) method instead, like:
 if(operator.getText().equals("+")){...}


As for:

Quote

My program went wrong, I'm not sure what kind of error I have

Do you get any error messages? if yes please post the exact messages you get.
Or, is that a logic error? what is it then?
Was This Post Helpful? 2
  • +
  • -

#8 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 20 November 2010 - 06:35 PM

When I run the program in command prompt, appletviewer, I get this error message:

java.lang.NumberFormatException: empty String
at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:994)
at java.lang.Double.parseDouble(Double.java:510)
at AnAppletWithTextFields.init(AnAppletWithTextFields.java:33)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at kava.lang.Thread.run(Thread.java:662)
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from text fields?

Posted 20 November 2010 - 07:08 PM

Java run time is very kind to you :) It even provided you with the exact line number

at AnAppletWithTextFields.init(AnAppletWithTextFields.java:33)

double value1 = Double.parseDouble(textField1.getText());

your JTextField is empty thta's all
Was This Post Helpful? 2
  • +
  • -

#10 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 20 November 2010 - 08:01 PM

That fixed the problem! I have a different problem involving my calc method. It doesn't seem to detect any operations (+, -, *, /). I'm not sure how to fix this. Other than that, the program seems to work fine.
Was This Post Helpful? 0
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from text fields?

Posted 20 November 2010 - 09:40 PM

View Postmchin131, on 20 November 2010 - 09:01 PM, said:

That fixed the problem! I have a different problem involving my calc method. It doesn't seem to detect any operations (+, -, *, /). I'm not sure how to fix this. Other than that, the program seems to work fine.

Where in your code do you react the + - * / entered ?
Was This Post Helpful? 1
  • +
  • -

#12 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 21 November 2010 - 04:02 AM

View Postpbl, on 20 November 2010 - 10:40 PM, said:

View Postmchin131, on 20 November 2010 - 09:01 PM, said:

That fixed the problem! I have a different problem involving my calc method. It doesn't seem to detect any operations (+, -, *, /). I'm not sure how to fix this. Other than that, the program seems to work fine.

Where in your code do you react the + - * / entered ?


Sorry, I thought I posted it. This is my revised code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class AnAppletWithTextFields extends Applet implements ActionListener {
	public void init() {
		Label label1 = new Label("First text field: ");

		textField1 = new TextField("10");

		swapEm = new Button("Swap");
		swapEm.addActionListener(this);

		Label label2 = new Label("Second text field: ");

		textField2 = new TextField("20");
		textField2.setEditable(false);

		clear = new Button("Clear");
		clear.addActionListener(this);

		operand1 = new TextField("0");
		operator = new TextField("+");
		operand2 = new TextField("0");

		Label resultLabel = new Label("Result: ");
		resultField = new TextField("");
		resultField.setEditable(false);

		calc = new Button("Calculate");
		calc.addActionListener(this);

		double value1 = Double.parseDouble(textField1.getText());
		double value2 = Double.parseDouble(textField2.getText());

		int intOper1 = Integer.parseInt(operand1.getText());
		int intOper2 = Integer.parseInt(operand2.getText());


		add(label1);
		add(textField1);
		add(swapEm);
		add(label2);
		add(textField2);
		add(clear);
		add(operand1);
		add(operator);
		add(operand2);
		add(resultLabel);
		add(resultField);
		add(calc);
	}

	public void actionPerformed(ActionEvent e) {

		if(value1 > value2) {
		String temp = textField1.getText();
		textField1.setText(textField2.getText());
		textField2.setText(temp);
		}

		if (e.getSource() == clear) {
		textField1.setText("");
		textField2.setText("");
		}

		if (e.getSource() == calc) {

			if (operator.getText() == "+") {
			totalresult = inOper1 + intOper2;
			}

			if (operator.getText() == "-") {
			totalresult = intOper1 - intOper2;
			}
			if (operator.getText() == "*") {
			totalresult = intOper1 * intOper2;
			}
			if (operator.getText() == "/") {
			totalresult = intOper1 / intOper2;
			} else {
			System.err.println("Invalid operator");
			}
		System.out.println(totalresult);
		}
		repaint();
	}

	int intOper1, intOper2, totalresult;
	double value1, value2;
	TextField textField1, textField2, operand1, operand2, operator, resultField;
	Button swapEm, clear, calc;
}


Was This Post Helpful? 0
  • +
  • -

#13 MrLuke187  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 81
  • View blog
  • Posts: 237
  • Joined: 09-July 10

Re: Get value from text fields?

Posted 21 November 2010 - 05:42 AM

As japanir said above, use the String.equals(String s);-Method to compare your Strings.

Also, you should use one if-Block with other else if blocks in it, than using four if-Blocks alone. Like this:

if (e.getSource() == calc) {

			if (operator.getText().equals("+")) {
			totalresult = inOper1 + intOper2;
			} else if (operator.getText().equals("-")) {
			totalresult = intOper1 - intOper2;
			} else if (operator.getText().equals("*")) {
			totalresult = intOper1 * intOper2;
			} else if (operator.getText().equals("/")) {
			totalresult = intOper1 / intOper2;
			} else {
			System.err.println("Invalid operator");
			}
		System.out.println(totalresult);
		}



Greetings: Luke
Was This Post Helpful? 0
  • +
  • -

#14 mchin131  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 57
  • Joined: 23-September 10

Re: Get value from text fields?

Posted 21 November 2010 - 08:32 PM

Thank you for the help! One thing I also want to know is how to print the result to a label? I'm supposed to print the result into the label field named resultField, but I'm not sure how to do that.
Was This Post Helpful? 0
  • +
  • -

#15 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Get value from text fields?

Posted 21 November 2010 - 08:38 PM

View Postmchin131, on 21 November 2010 - 09:32 PM, said:

Thank you for the help! One thing I also want to know is how to print the result to a label? I'm supposed to print the result into the label field named resultField, but I'm not sure how to do that.

label.setText("" + result):
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2