Hi All,

I was having trouble with getting my program to display all the necessary conversions. Here is a snippet of the code to help explain what I want to do:

private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput, inchInput, meterInput, yardInput;
			double cent, inch, meter, yard;
			
			yardInput = yardsText.getText();
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));

This will allow me to put an input into the "Yards" Textfield so that when I click calculate, the inch, cent, and meter Textfields will display their proper conversions. So my question is, How would I be able to make an input in the "Inch" Textfield, so that when I click the calculate button it gives all the conversions to the other units of measurements?(I need to be able to put an input into any textfield and get the proper conversions in the other ones.) But If I put similar code to the one I posted for the different input, I just get errors. I hope that I explained my question thoroughly enough. Thanks for any help in advance!

Recommended Answers

All 36 Replies

I just get errors

If you copy and paste the error messages here someone can help you.

How would I be able to make an input in the "Inch" Textfield,

Are you talking about the program changing the text field or about the user doing it?
The text field class has a method to set the text field's value. I don't know how you can make a user type into the text field.

Thanks for the reply! I am talking about the user changing input and being able to put input into any textbox that they please and being able to get the proper conversions to display into the other textfields. I have changed my code around to try to figure it out so here is my new code:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class ConversionCalculator extends JFrame{

	final int WINDOW_WIDTH = 900;
	final int WINDOW_HEIGHT = 200;
	
	private JPanel panel1, panel2, panel3;
	private JLabel centLabel, inchLabel, meterLabel, yardsLabel;
	private JTextField centText, inchText, meterText, yardsText;
	private JButton clear, calc, exit;
	private Container pane;
	
	public ConversionCalculator(){
		setTitle("Conversion Calculator");
		setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pane = getContentPane();
		pane.setLayout(new GridLayout(1,3));
		buildPanel();
		pane.add(panel1);
		pane.add(panel2);
		pane.add(panel3);
		setVisible(true);
	}
	
	private void buildPanel(){
		
		centLabel = new JLabel("Centimeters ", SwingConstants.LEFT);
		inchLabel = new JLabel("Inches ", SwingConstants.CENTER);
		meterLabel = new JLabel("Meters", SwingConstants.LEFT);
		yardsLabel = new JLabel("Yards", SwingConstants.CENTER);
		
		centText= new JTextField(8);
		inchText= new JTextField(8);
		meterText = new JTextField(8);
		yardsText = new JTextField(8);
		
		clear = new JButton("Clear");
		calc = new JButton("Calculate");
		exit = new JButton("Exit");
		
		clear.addActionListener(new ClearListener());
		calc.addActionListener(new CalcListener());
		exit.addActionListener(new ExitListener());
		
		panel1 = new JPanel();
		panel1.setLayout(new GridLayout(2,2));
		panel1.add(centLabel); panel1.add(centText); 
		panel1.add(meterLabel); panel1.add(meterText); 
		
		panel2 = new JPanel();
		panel2.setLayout(new GridLayout(2,2));
		panel2.add(inchLabel); panel2.add(inchText);
		panel2.add(yardsLabel); panel2.add(yardsText);
		
		panel3 = new JPanel();
		panel3.setLayout(new GridLayout(3,1));
		panel3.add(clear);
		panel3.add(calc);
		panel3.add(exit);
	}
	
	private class ClearListener implements ActionListener{
		public void actionPerformed(ActionEvent d){
			centText.setText("");
			inchText.setText("");
			meterText.setText("");
			yardsText.setText("");
		}
	}
	private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput == yardsText.getText()){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}
				
			if(inchInput == inchText.getText()){
			cent = Double.parseDouble(inchInput)* 2.54;
			meter = Double.parseDouble(inchInput)* 0.0254;
			yard = Double.parseDouble(inchInput) * 0.0278;
			
			yardsText.setText(String.valueOf(yard));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}
			
			if(centInput == centText.getText()){
			inch = Double.parseDouble(centInput) * 0.393701;
			meter = Double.parseDouble(centInput)* 0.01;
			yard = Double.parseDouble(centInput) * 0.010936;
			
			inchText.setText(String.valueOf(inch));
			yardsText.setText(String.valueOf(yard));
			meterText.setText(String.valueOf(meter));
			}
			
			if(meterInput == meterText.getText()){
			inch = Double.parseDouble(meterInput) * 39.370079;
			cent = Double.parseDouble(meterInput)* 100.00;
			yard = Double.parseDouble(meterInput) * 1.093613;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			yardsText.setText(String.valueOf(yard));
			}
		}
	}
	
	private class ExitListener implements ActionListener{
		
		public void actionPerformed(ActionEvent f){
			System.exit(0);
		}
	}
}

Here is the driver class:

public class ConversionCalculatorDriver {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ConversionCalculator convCalc = new ConversionCalculator();

	}

}

The problem I am now having is that the user inputs a number into any textfield and pushes the "calculate" button, but no conversions are displaying into the other textfields. Also, no more error codes/messages now. Thanks for help again in advance!

The problem I am now having is that the user inputs a number into any textfield and pushes the "calculate" button, but no conversions are displaying into the other textfields

Can you detect which text field the user entered his data in?
Given that, you should then know which of the other fields need to be updated.

Please explain what you want this statement to do:

if(yardInput == yardsText.getText()){

I am guessing that my code does not detect which text field the user entered their data in, because I do not know how to do that. I was trying to accomplish that with the if statements, but I do not think I did it properly, as it lets me type input into any of the textfields but does not display anything into the other textfields after the "calculate" button is pushed. How can I properly detect which textfield has input in it? Thanks again!

Look at the code in the actionListener method for the calc button.
What does that code do?
What do you want it to do?

Please explain what you want this statement to do:

if(yardInput == yardsText.getText()){

In the actionListener method: I want to it check which box has input in it and when the calculate button is pushed I want it convert the input into the other units of measurement and display them in the appropriate text field. But my code is not doing much of anything because nothing displays when I click calculate.

That if statement you took from my code: I would like it to check and see if there is input in that text field and if there is I would like it to use the following code to do the conversions and then display them in the other textfields.

Thanks again.

I would like it to check and see if there is input in that text field

The statement is comparing a variable with a null value to reference to a String variable that is returned by the getText() method.
First problem here is that the == operator tests if two variables refer to the same object, not if two different objects contain the same String.
You should use the equals method to compare Strings.

If the getText method returns a null and the if statement is true, then what?
Where do you get the data from the text field for the computations that are done inside of the if statement?

So I would need to do a scanner to see what input is entered in the text field to read what data I am doing the conversions with?

The getText() method returns what input is entered in the text field.

The Scanner class is for reading from the console or from a disk file.

Also you could read in the tutorial about text fields.
Go to this site and Find Text:
http://download.oracle.com/javase/tutorial/reallybigindex.html
There are several topics listed include one for Text Fields.

Ok, so if I implement the equals method:

private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput.equals(yardsText.getText())){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}

then it would compare the input with what is in the textfield and if there is something entered in the textfield it will compute the conversions and display. This is what I am hoping it to do. But I get null errors as follows:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:81)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:81)

What variable on line 81 is null? When you see which one, back track in your code to see why it is null.

If you can not see which one, Add a println statement just before line 81 that prints out the values of all the variables on line 81.


Look at line 4 in the code you just posted.
Where do any of the variables defined in that line get values?

For debugging add some printlns at the very beginning of the listener method to show what are in the text fields. Something like this for all the text fields:

System.out.println("yardsText.getText()=" + yardsText.getText() + "<");

if you are using if statements then I would suggest you store the last value in each textfield then you compare them to see if they are not equal (has been changed) then you can recalculate the rest of the values using the one you used in the if statement. Then all you would need to do would be to set the new value into the text fields.

@ NormR1:

I added some printlns to see the values in the textfield:

public void actionPerformed(ActionEvent e){
			String centInput = null, inchInput = null, meterInput = null, yardInput = null;
			double cent=0, inch=0, meter=0, yard=0;
			
			//Test for output
			System.out.println("yardsText.getText()=" + yardsText.getText() + "<");
			System.out.println("inchText.getText()=" + inchText.getText() + "<");
			System.out.println("meterText.getText()=" + meterText.getText() + "<");
			System.out.println("centText.getText()=" + centText.getText() + "<");
			
			if(yardInput.equals(yardsText.getText())){
			inch = Double.parseDouble(yardInput) * 36.00;
			cent = Double.parseDouble(yardInput)* 91.44;
			meter = Double.parseDouble(yardInput) * 0.9144;
			
			inchText.setText(String.valueOf(inch));
			centText.setText(String.valueOf(cent));
			meterText.setText(String.valueOf(meter));
			}

and this is the result:

yardsText.getText()=10<Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:87)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)

inchText.getText()=<
meterText.getText()=<
centText.getText()=<
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

All it prints is what the user puts as input into whichever textfield they enter input to, and the other 3 values there is no data for. How can I use the results from the printllns to help me figure out what I need to do.
Thanks for the help once again!

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:87)

What variable is null at line 87?

Again: What are you testing here:

if(yardInput.equals(yardsText.getText())){

What is the value of yardInput?
If the equals returns true, you use yardInput on the next line:

inch = Double.parseDouble(yardInput) * 36.00;

How did it get a value that you want to parse?

yardInput is null @ line 87.

if(yardInput.equals(yardsText.getText())){

:
Here I am testing if yardInput equals what the user put into the textfield.

inch = Double.parseDouble(yardInput) * 36.00;

:
I cannot get a value that I want to parse because all of the conversions are based off of the user input, so there are no values being given to it, because inch is initialized as a 0.

You can NOT call a method using a null variable. Here equals() is a method of the String class, but yardInput is null and this will NOT work.
yardInput.equals(

On this line you would attempt to convert the contents of yardInput to a double value.
inch = Double.parseDouble(yardInput) * 36.00;
But you NEVER put anything into the yardInput variable.

Neither of these statements will ever work unless you put a value into yardInput.

yardInput will not magically get a value. You must write an assignment statement that puts a value into it.

Ok, I get what you are saying about me trying to use these without values. I corrected it and it works fine if I input a value into the "yards" textfield. I get an empty string error though if user puts a value into any textfield other than the "yards" text field, as seen here:

Exception in thread "AWT-EventQueue-0" java.lang.NumberFormatException: empty String
	at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:992)
	at java.lang.Double.parseDouble(Double.java:510)
	at ConversionCalculator$CalcListener.actionPerformed(ConversionCalculator.java:85)
	at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
	at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
	at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
	at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
	at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
	at java.awt.Component.processMouseEvent(Component.java:6373)
	at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
	at java.awt.Component.processEvent(Component.java:6138)
	at java.awt.Container.processEvent(Container.java:2085)
	at java.awt.Component.dispatchEventImpl(Component.java:4735)
	at java.awt.Container.dispatchEventImpl(Container.java:2143)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
	at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
	at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
	at java.awt.Container.dispatchEventImpl(Container.java:2129)
	at java.awt.Window.dispatchEventImpl(Window.java:2478)
	at java.awt.Component.dispatchEvent(Component.java:4565)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
	at java.awt.EventQueue.access$000(EventQueue.java:85)
	at java.awt.EventQueue$1.run(EventQueue.java:638)
	at java.awt.EventQueue$1.run(EventQueue.java:636)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
	at java.awt.EventQueue$2.run(EventQueue.java:652)
	at java.awt.EventQueue$2.run(EventQueue.java:650)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

This is how I have a portion of my code written now:

public void actionPerformed(ActionEvent e){
			String centInput = centText.getText() , inchInput = inchText.getText(), meterInput = meterText.getText(), 
								yardInput = yardsText.getText();
			
			double cent=0, inch=0, meter=0, yard=0;
			
			if(yardInput.equals(yardsText.getText()))
			{
				yardInput.equals(Double.parseDouble(yardsText.getText()));
				inch = Double.parseDouble(yardInput) * 36.00;
				cent = Double.parseDouble(yardInput) * 91.44;
				meter = Double.parseDouble(yardInput) * 0.9144;
			
				inchText.setText(String.valueOf(inch));
				centText.setText(String.valueOf(cent));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("yardsText.getText()=" + yardsText.getText() + "<");
				System.out.println("inchText.getText()= " + inch + "<");
				System.out.println("meterText.getText()= " + meter + "<");
				System.out.println("centText.getText()= " + cent + "<");
			}
			
			else if(inchInput.equals(inchText.getText()))
			{
				inchInput.equals(Double.parseDouble(inchText.getText()));
				cent = Double.parseDouble(inchInput)* 2.54;
				meter = Double.parseDouble(inchInput)* 0.0254;
				yard = Double.parseDouble(inchInput) * 0.0278;
			
				yardsText.setText(String.valueOf(yard));
				centText.setText(String.valueOf(cent));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("inchText.getText()= " + inchText.getText() + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("meterText.getText()= " + meter + "<");
				System.out.println("centText.getText()= " + cent + "<");
			}

			else if(centInput.equals(centText.getText()))
			{
				centInput.equals(Double.parseDouble(centText.getText()));
				inch = Double.parseDouble(centInput) * 0.393701;
				meter = Double.parseDouble(centInput)* 0.01;
				yard = Double.parseDouble(centInput) * 0.010936;
			
				inchText.setText(String.valueOf(inch));
				yardsText.setText(String.valueOf(yard));
				meterText.setText(String.valueOf(meter));
				
				//Test for output
				System.out.println("centText.getText()= " + centText.getText()  + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("inchText.getText()= " + inch+ "<");
				System.out.println("meterText.getText()= " + meter + "<");
			}

			else //if(meterInput.equals(meterText.getText()))
			{
				meterInput.equals(Double.parseDouble(meterText.getText()));
				inch = Double.parseDouble(meterInput) * 39.370079;
				cent = Double.parseDouble(meterInput)* 100.00;
				yard = Double.parseDouble(meterInput) * 1.093613;
			
				inchText.setText(String.valueOf(inch));
				centText.setText(String.valueOf(cent));
				yardsText.setText(String.valueOf(yard));
				
				//Test for output
				System.out.println("meterText.getText()= " + meterText.getText()  + "<");
				System.out.println("yardsText.getText()=" + yard + "<");
				System.out.println("inchText.getText()= " + inch+ "<");
				System.out.println("centText.getText()= " + cent + "<");
			}
			
		}
	}

I am trying to get it so that a user can insert an input into any of the four text fields and it converts the number into its perspective length and displays it in the appropriate textfield, but as of now it only works if the user puts a value into the "yards" textfield and not the others.
As always thanks for the help you have been giving me.

You need to work through your logic and consider what the if tests are doing.
The following code is similar to what your code is doing:
x = 1;
if (x == 1) {

With these two statements the if test will ALWAYS be true.
That is exactly what your code is doing with the text field contents.

You need to look at what you want to test for BEFORE you attempt to convert a text field's contents to a number.

I get what you are saying, my logic behind that test is very flawed. All I need to do is check that a user put a value in any one of the four text fields and then take that value and convert it and display it in the other text fields. How can I check to see what value is in a Jtextfield and use that value? Thanks again.

Look back at where you printed out the contents of all the text fields.
Where did those println statements get the data they printed?

It comes from

yardsText.getText()

So if I want to use this for the test condition in my if statements to see if the textfield has a value, I would use something like:

if(yardsText.getText().equals(true))

?

What does "true" test for? Did you try to compile that line of code? If not, you should try to compile your code before posting.

What do you want to know about the contents of the yardsText object?
Look at the API doc to see if any of the methods there would be useful.

I did run that code before posting, and it came up with another huge error list as usual with everything I write, was posting it so you could see that I am indeed trying to figure out my problem. I thought that the true would test to see that a value is in the textfield. I've been stuck on this same spot for roughly 6 hours and it is getting very frustrating for me ;/, but your help is much appreciated.

What do you want to know about the contents of the yardsText object?
- I want to know what the contents are whether it be in the"yards", "centimeter", "inches", or "meters" textfield so that I can do the conversions for the other textfields and then display them in their perspective textfields.

With the code I previously posted(with the flawed logic if statements) the program runs properly if the user enters data into the "yards" textfield, but does not work if they enter a value into the "inches", "centimeters", or "meters" textfield.

I want to know what the contents are

What if the text field has no contents? Is that of interest?
What methods does the text field class have that would help you detect if it is empty?

I thought that the true would test to see that a value is in the textfield.

Where does the API doc say that the value returned by getText() would be true or false?

Ok NormR1, we have a solution!

The major problem was, like you were saying my comparisons. I had nothing in the text fields to start with, so I could not do an appropriate conditional in my if statements. So, I set the values in the textfields to "0.00".

Before it looked like this:

centText= new JTextField(8);
		inchText= new JTextField(8);
		meterText = new JTextField(8);
		yardsText = new JTextField(8);

So, as mentioned above I set the default values in the textfield to "0.00" as seen here:

centText= new JTextField("0.00", 8);
		inchText= new JTextField("0.00", 8);
		meterText = new JTextField("0.00", 8);
		yardsText = new JTextField("0.00", 8);

Then, I was able to have a proper conditional in my if statement because it was actually able to compare it with something(I think this is the main point you were trying to get across to me):

private class CalcListener implements ActionListener{
		
		public void actionPerformed(ActionEvent e){
			String centInput = centText.getText() , inchInput = inchText.getText(), meterInput = meterText.getText(), 
			yardInput = yardsText.getText();
			double cent=0, inch=0, meter=0, yard=0;
			
			DecimalFormat beagle = new DecimalFormat("0.00");
			
			if(!yardsText.getText().equals("0.00"))
			{
				yardInput.equals(Double.parseDouble(yardsText.getText()));
				yard = Double.parseDouble(yardInput);
				inch = Double.parseDouble(yardInput) * 36.00;
				cent = Double.parseDouble(yardInput) * 91.44;
				meter = Double.parseDouble(yardInput) * 0.9144;
				
				yardsText.setText(String.valueOf(beagle.format(yard)));
				inchText.setText(String.valueOf(beagle.format(inch)));
				centText.setText(String.valueOf(beagle.format(cent)));
				meterText.setText(String.valueOf(beagle.format(meter)));
				
			}
			
			else if(!inchText.getText().equals("0.00"))
			{
				inchInput.equals(Double.parseDouble(inchText.getText()));
				inch = Double.parseDouble(inchInput);
				cent = Double.parseDouble(inchInput)* 2.54;
				meter = Double.parseDouble(inchInput)* 0.0254;
				yard = Double.parseDouble(inchInput) * 0.0278;
				
				inchText.setText(String.valueOf(beagle.format(inch)));
				yardsText.setText(String.valueOf(beagle.format(yard)));
				centText.setText(String.valueOf(beagle.format(cent)));
				meterText.setText(String.valueOf(beagle.format(meter)));
				
			}

			else if(!centText.getText().equals("0.00"))
			{
				centInput.equals(Double.parseDouble(centText.getText()));
				cent = Double.parseDouble(centInput);
				inch = Double.parseDouble(centInput) * 0.393701;
				meter = Double.parseDouble(centInput)* 0.01;
				yard = Double.parseDouble(centInput) * 0.010936;
			
				centText.setText(String.valueOf(beagle.format(cent)));
				inchText.setText(String.valueOf(beagle.format(inch)));
				yardsText.setText(String.valueOf(beagle.format(yard)));
				meterText.setText(String.valueOf(beagle.format(meter)));
				
			}

			else 
			{
				meterInput.equals(Double.parseDouble(meterText.getText()));
				meter = Double.parseDouble(meterInput);
				inch = Double.parseDouble(meterInput) * 39.370079;
				cent = Double.parseDouble(meterInput)* 100.00;
				yard = Double.parseDouble(meterInput) * 1.093613;
				
				meterText.setText(String.valueOf(beagle.format(meter)));
				inchText.setText(String.valueOf(beagle.format(inch)));
				centText.setText(String.valueOf(beagle.format(cent)));
				yardsText.setText(String.valueOf(beagle.format(yard)));

			}
			
		}
	}

Thanks for all of the help again!

yardInput.equals(Double.parseDouble(yardsText.getText()));

What does this statement do? The equals method returns a boolean value that you are ignoring.
This looks like a useless statement. And in the other if statements also.

@javaNooblet

consider using JFormattedTextField instead of JTextField, example about how to use Number instance included

getValue() from JFormattedTextField is in form

((Number)amountField.getValue()).doubleValue();

@NormR1:

yardInput.equals(Double.parseDouble(yardsText.getText()));

I used this statement so that it would convert the string input in textfield to a double, so that I could then use that input for the conversions of the other units of measurement.

@mKorbel:

((Number)amountField.getValue()).doubleValue();

I did not know this existed(I'm new :/). I will try this, its seems like it would give me the result I wanted easier.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.