This page contains an archived post to the Java Answers Forum made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Newbie: = operator
Posted by KABOOM on January 16, 2002 at 12:08 AM
I am now 4 days into Java programming and i'm wondering why this = operator doesn't work for my program. Basically, it says they are incompatible types. What the heck does that mean? I've added my code below (i've outlined the problem section in my code below. Look at my playersalary method with in my class salary. There's a section labelled "WHAT's UP WITH THIS?????" Thanks, KABOOM public class baseball extends JFrame implements ActionListener { JButton Griffey = new JButton("Griffey Jr"); JButton ARod = new JButton("A-Rod"); JButton Bonds = new JButton("Bonds"); JTextField mychoice = new JTextField(); // CONSTRUCTOR public baseball() { super("Who's the best overpaid bum?"); // Add title setSize(500, 500); // Listeners for actions Griffey.addActionListener(this); ARod.addActionListener(this); Bonds.addActionListener(this); mychoice.addActionListener(this); // Layout Properties JPanel pane = new JPanel(); pane.setLayout(new BorderLayout()); pane.add("West", Griffey); pane.add("Center", ARod); pane.add("East", Bonds); pane.add("South", mychoice); mychoice.setEditable(false); setContentPane(pane); } public void actionPerformed(ActionEvent someevent) { Object choice = someevent.getSource(); salary overpaid = new salary(); String phrase = " is a highly paid bum!"; Integer dollars;
if(choice == Griffey) { dollars = overpaid.playersalary("Griffey"); mychoice.setText("Griffey Jr. at " + dollars + phrase); } else if(choice == ARod) { dollars = overpaid.playersalary("ARod"); mychoice.setText("ARod at " + dollars + phrase); } else if(choice == Bonds) { dollars = overpaid.playersalary("Bonds"); mychoice.setText("Bonds at " + dollars + phrase); } else { mychoice.setText("Invalid selection"); } repaint(); } // MAIN public static void main(String[] args) { JFrame someframe = new baseball(); // WILL END application when window closed ExitWindow exit = new ExitWindow(); someframe.addWindowListener(exit);
someframe.pack(); someframe.setVisible(true); } } class salary { Integer wage; public Integer playersalary(String name) { if(name == "ARod") wage = 252; // WHATS UP WITH THIS???????????? else if(name == "Bonds") wage = 90; // WHATS UP WITH THIS???????????? else if(name == "Griffey") wage = 100; // WHATS UP WITH THIS???????????? else wage = 0; // WHATS UP WITH THIS????????????
return wage; } } class ExitWindow extends WindowAdapter { public void windowClosing(WindowEvent e) { System.exit(0); } }
Replies:
|