The Opeartor * is undefined

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 2265 Views - Last Post: 21 February 2011 - 06:43 PM Rate Topic: -----

#1 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

The Opeartor * is undefined

Posted 21 February 2011 - 01:48 PM

Hello everyone! I'm kind of new to Java, and I'm trying to build a simple application for myself to do basic imperial to metric conversions for weight, length, and temperature (I'll probably expand in the future, but I want to do these ones for now). I've got the UI built, and I'm now working on the logic of the calculations, which to me, seem easy enough, but I keep getting an error of "The operator * is undefined for the argument type(s), JTextField, double", and I can't for the life of me, figure out why it's doing that. Here's what I've got, and I'll post the actual line of code again sepeartely:

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
	 
	public class MetricConverterApp extends JFrame implements ActionListener
	{
		public MetricConverterApp() 
	    {
			
			double kilogram = 0.45359;
			
	         //Title section   
	        setTitle("Metric Converter"); 
	        setSize(300,150);
	        setVisible(true);
	        setResizable(false);
	        
	        JTabbedPane jtp = new JTabbedPane();
	        
	        //Builds template of the window
	       getContentPane().add(jtp);
	 
	     //creating an instance of the menu bar for across the program
			MenuBar mnuBar = new MenuBar();
			setMenuBar(mnuBar);
			
			//building and filling the program's File menu
			Menu mnuFile = new Menu("File", true);
			mnuBar.add(mnuFile);
				MenuItem mnuFileExit = new MenuItem("Exit");
				mnuFile.add(mnuFileExit);
				
				
			//building and filling the program's About menu
			Menu mnuAbout = new Menu("About", true);
			mnuBar.add(mnuAbout);
				MenuItem mnuAboutMetricConverter = new MenuItem("About Metric Converter");
				mnuAbout.add(mnuAboutMetricConverter);
				
			//adding ActionListener to each of the menu items
			mnuFileExit.addActionListener(this);
			mnuAboutMetricConverter.addActionListener(this);
			
			//give each menu item an ActionCommand
			mnuFileExit.setActionCommand("Exit");
			mnuAboutMetricConverter.setActionCommand("About");
			
			
	       JPanel jp1 = new JPanel();//Builds first tab
	       jp1.setLayout(new BorderLayout());//Sets up layout of first tab
	       JPanel jp2 = new JPanel();//Builds second tab
	       jp2.setLayout(new BorderLayout());//Sets up layout of second tab
	       JPanel jp3 = new JPanel();//Builds third tab
	       jp3.setLayout(new BorderLayout());//sets up layout of second tab
	       

	       //Builds prompt for first tab
	       JLabel weightPrompt = new JLabel();
	       weightPrompt.setText("Enter the weight in pounds");
	       jp1.add(weightPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for first tab
	       JTextField weightInput = new JTextField();
	       jp1.add(weightInput, BorderLayout.CENTER);
	       
	       //Builds button for first tab
	       JButton metWeight = new JButton("Convert to kilograms");
	       jp1.add(metWeight, BorderLayout.EAST);
	       metWeight.addActionListener(this);
	       
	       //Builds text output for first tab
	       JTextField weightOutput = new JTextField();
	       jp1.add(weightOutput, BorderLayout.SOUTH);
	       
	       
	       //Builds prompt for second tab
	       JLabel lengthPrompt = new JLabel();
	       lengthPrompt.setText("Enter the length in feet");
	       jp2.add(lengthPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for first tab
	       JTextField lengthInput = new JTextField("");
	       jp2.add(lengthInput, BorderLayout.CENTER);
	       
	       //Builds button for second tab
	       JButton metLength = new JButton("Convert to meters");
	       jp2.add(metLength, BorderLayout.EAST);
	       
	       //Builds text output for second tab
	       JTextField lengthOutput = new JTextField();
	       jp2.add(lengthOutput, BorderLayout.SOUTH);
	       
	       
	       //Build prompt for third tab
	       JLabel tempPrompt = new JLabel();
	       tempPrompt.setText("Enter the temperature in farenheit");
	       jp3.add(tempPrompt, BorderLayout.NORTH);
	       
	       //Builds text input for third tab
	       JTextField tempInput = new JTextField("");
	       jp3.add(tempInput, BorderLayout.CENTER);
	       
	       //Builds button for third tab
	       JButton metTemp = new JButton("Convert to celcius");
	       jp3.add(metTemp, BorderLayout.EAST);
	       
	       //Builds text output for third tab
	       JTextField tempOutput = new JTextField();
	       jp3.add(tempOutput, BorderLayout.SOUTH);
	 
	       //Adds tabs to pane
	       jtp.addTab("Weight", jp1);
	       jtp.addTab("Length", jp2);
	       jtp.addTab("Temperature", jp3);
	       
      		Integer.parseInt(weightInput.getText());

       		weightOutput.setText(weightInput * kilogram);
	    }
	     
		public void actionPerformed(ActionEvent e) 
		{
			//ActionListener for menu item clicks
       		String arg = e.getActionCommand();
       		if (arg == "Exit")
       			System.exit(0);
       		
       		if (arg == "About")
       		{
       			String message = "Metric Converter v1.0\nDesigned by:\nMy Name Here\nFebruary 19, 2011";
       			JOptionPane.showMessageDialog(null,message,"About Metric Converter", JOptionPane.INFORMATION_MESSAGE);
       		}
		}
	 
		//main method
	     public static void main (String []args)
	     {
	        MetricConverterApp tab = new MetricConverterApp();
	        
	    }
}


Problem line right here (line 123):
weightOutput.setText(weightInput * kilogram);


The code might look a bit sloppy in some areas, and it may just look kind of tossed together overall, but I really want to just get it working first. I'd appreciate any help, thanks!

This post has been edited by iceregulus: 21 February 2011 - 01:49 PM


Is This A Good Question/Topic? 0
  • +

Replies To: The Opeartor * is undefined

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: The Opeartor * is undefined

Posted 21 February 2011 - 01:56 PM

The weightInput variable is a JTextField, not a number. You could use the Double.parseDouble() method to convert its text (see the JTextField getText() method) to a double that can be multiplied.

Not good practice to use Action Commands. Better to declare your JButtons as instance variables, and compare them to the source of the ActionEvent (see e.getSource()).
String arg = e.getActionCommand(); 



Also, not good practice to compare Strings using the == operator, as it compares their locations in memory. Use the String equals() method instead which compares their values.
if (arg == "Exit") 


Was This Post Helpful? 0
  • +
  • -

#3 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 02:44 PM

Ok, I did what you said for the parseDouble method here, above the line with the error:

double value = Double.parseDouble(weightInput.getText());


But I'm still getting the same issue...I get the feeling that there something ridiculously simple that I forgot or am not doing right.
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: The Opeartor * is undefined

Posted 21 February 2011 - 02:47 PM

You need to multiply value * kilograms, not weightInput (which is a JTextField).
Was This Post Helpful? 0
  • +
  • -

#5 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 02:56 PM

Ah, yes! Ok, great, got that part taken care of, but now a new error shows up, haha. The setText bit right next to it throws me an error of "The method setText(String) in the type JTextComponent is not applicable for the arguments(double)". So, does that mean I can't use setText for double values?
Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: The Opeartor * is undefined

Posted 21 February 2011 - 02:59 PM

You can easily use the + "" expression to convert a double to a String:
double d = 3.14;
String s = d + "";


Was This Post Helpful? 0
  • +
  • -

#7 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:17 PM

Can't I just have the output a double? I understand that the input begins as a string, and needs to be converted to a double, but it just seems to me to make more sense to just allow it to output as whatever, double, string, int, etc. So would that code snippet you posted be used in the weightOutput part? Or is that something all by itself? Sorry for the constant questions, I'm still learning this stuff.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:23 PM

You have to convert the double back to a String to use the JTextField or JTextArea setText() method.
Was This Post Helpful? 0
  • +
  • -

#9 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:30 PM

Ok, and it is the double that was labeled "value" that I have to get back to a String, right? So then the code would be this:

double value = Double.parseDouble(weightInput.getText());
weightOutput.setText(value * kilogram);
String s = value + "";


If it's not that, then it has to be either weightInput, weightOutput, or kilogram...I can't think of what else it could be?
Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10467
  • View blog
  • Posts: 38,794
  • Joined: 27-December 08

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:32 PM

How about just: weightOutput.setText((value * kilograms) + "");. This insures the mathematical operation is calculated first, then converted to a String to be displayed by your JComponent.
Was This Post Helpful? 0
  • +
  • -

#11 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:41 PM

Or you could use the String.valueOf() method.

This post has been edited by m-e-g-a-z: 21 February 2011 - 03:42 PM

Was This Post Helpful? 0
  • +
  • -

#12 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 03:50 PM

Hmm, well that worked...I feel kind of dumb. The program runs now, but when I enter a value and hit "Convert to Kilograms", nothing happens. I think I need to add an ActionListener event to my button(s), and I found another thread on another site ( http://www.coderanch...riving-me-crazy ), where it was suggested to do this:

# JButton button = new JButton("Push Me");  
# button.addActionListener(new ActionListener()  
# {  
#    public void actionPerformed(ActionEvent e)  
#    {  
#       //Do your stuff here  
#    }  
# }); 


So, would I need to basically need to add the button.addActionListener inside where my actionPerformed method is down near the bottom? And then from there, just paste the code we were working on inside where it says //Do your stuff here ? That seems to be the case.

This post has been edited by iceregulus: 21 February 2011 - 03:59 PM

Was This Post Helpful? 0
  • +
  • -

#13 m-e-g-a-z  Icon User is offline

  • Winning
  • member icon


Reputation: 496
  • View blog
  • Posts: 1,453
  • Joined: 19-October 09

Re: The Opeartor * is undefined

Posted 21 February 2011 - 04:12 PM

Since your class implements the ActionListener interface, you can simply do this.

button.addActionListener(this);



Then override the ActionPerformed method like this.

@Override
public void actionPerformed(ActionEvent e) {
	
	if(e.getSource()==button){
		//do something
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#14 iceregulus  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 21-February 11

Re: The Opeartor * is undefined

Posted 21 February 2011 - 04:24 PM

Ok so I added the first bit to the set of code where I create the button itself:
	       //Builds button for first tab
	       JButton metWeight = new JButton("Convert to kilograms");
	       jp1.add(metWeight, BorderLayout.EAST);
	       metWeight.addActionListener(this);


And I added the piece of code we were working on earlier to the actionPerformed section, with the override above it:

if(e.getSource()== metWeight)
{
double value = Double.parseDouble(weightInput.getText());
weightOutput.setText((value * kilogram) + "");
}


And now, metWeight and kilogram are getting errors that they "Cannot be resolved to a variable", and weightInput and weightOutput are getting errors of "Cannot be resolved". I don't have to make a new set of variables inside the actionPerformed method do I? Or is that section of code even suppose to be in there?

This post has been edited by iceregulus: 21 February 2011 - 04:25 PM

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: The Opeartor * is undefined

Posted 21 February 2011 - 05:27 PM

This is obviously in a method or in the constructor

JButton metWeight = new JButton("Convert to kilograms");

so metWeight will stop to have a reference/exists as soon you will exit the method/constructor;

You will have to put it in the instance variables for the actionPerformed() method to see it

class MyClass {
   JButton metWeight;     // <---- here

   MyClass() {     <---- constructor
     ...
     metWeight = new JButton("Convert to kilograms");   // init instance variable


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2