Where are they defined? Are they in scope (within the same pair of {}s) as where they are being referenced?
Printable View
Where are they defined? Are they in scope (within the same pair of {}s) as where they are being referenced?
Code :public class ClickButtonListener implements ActionListener { public void actionPerformed(ActionEvent e) { Object o = e.getSource(); if(o==aspen) { if(aspen.isSelected()) { int hprice = 100000; } } else if(o==brittany) { if(brittany.isSelected()) { int hprice = 120000; } } else if(o==colonial) { if(colonial.isSelected()) { int hprice = 180000; } } else if(o==dartmoor) { if(dartmoor.isSelected()) { int hprice = 250000; } } else if(o==twobed) { if(twobed.isSelected()) { int bprice = 21000; } } else if(o==threebed) { if(threebed.isSelected()) { int bprice = 31500; } } else if(o==fourbed) { if(fourbed.isSelected()) { int bprice = 42000; } } else if(o==carzero) { if(carzero.isSelected()) { int cprice = 0; } } else if(o==carone) { if(carone.isSelected()) { int cprice = 7775; } } else if(o==cartwo) { if(cartwo.isSelected()) { int cprice = 15550; } } else if(o==carthree) { if(carthree.isSelected()) { int cprice = 23325; } } // price.setText(+hprice+bprice+cprice); } }
i defined them in my if statements.
im not sure if i should keep "Object o = e.getSource();" or use String o;
Is the program working now?
yes thank you i got it to work, but now i have trouble with another program.
how do i add the cost of the pizza's size into my output?
Code :***********Description - this program uses jcomboboxes to let the user choose a pizza size, and toppings. *********then it will show the price of the pizza along with a picture of a pizza. import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JPizza extends JFrame { public static void main (String[] arg) { JPizza window = new JPizza(); window.setVisible(true); } final int SMALL = 7; final int MEDIUM = 9; final int LARGE = 11; final int XLARGE = 14; // final int topPrice = 1; JLabel price = new JLabel(""); JLabel title = new JLabel("Order a pizza"); JComboBox sizeBox; JComboBox toppingBox; public JPizza() { ImageIcon pizzaPic = new ImageIcon("pizza.jpg"); JLabel pizzaLabel = new JLabel(pizzaPic); setTitle("Pizzaria"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,350); Container pane = getContentPane(); pane.setLayout(new FlowLayout()); sizeBox = new JComboBox(); String[] sizeArray = {"Small","Medium","Large","XLarge"}; sizeBox = new JComboBox(sizeArray); pane.add(sizeBox); String[] toppingArray = {"Cheese","Mushrooms","Pepperoni","Bacon","Chicken"}; toppingBox = new JComboBox(toppingArray); pane.add(toppingBox); pane.add(price); pane.add(title); pane.add(pizzaLabel); ClickButtonListener clickListener = new ClickButtonListener(); sizeBox.addActionListener(clickListener); toppingBox.addActionListener(clickListener); } public class ClickButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { int cost = 0; int topCost = 0; int sizeChoice = sizeBox.getSelectedIndex(); cost = sizeCost(sizeChoice); Object toppingChoice = toppingBox.getSelectedItem(); if(toppingChoice.equals("Cheese")) topCost = 1; cost += 1; price.setText("Your pizza costs $"+cost); } } } --------------------Configuration: <Default>-------------------- K:\CompSci\Projects\JPizza.java:64: error: cannot find symbol cost = sizeCost(sizeChoice); ^ symbol: method sizeCost(int) location: class JPizza.ClickButtonListener Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error Process completed.
Where is the pizza's size determined? How can/does the program get the size?Quote:
how do i add the cost of the pizza's size into my output
When it gets the size, how does it get the cost?
Where is the output that the cost should be added to?
Code :import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JPizza extends JFrame { public static void main (String[] arg) { JPizza window = new JPizza(); window.setVisible(true); } final int SMALL = 7; final int MEDIUM = 9; final int LARGE = 11; final int XLARGE = 14; // final int topPrice = 1; JLabel price = new JLabel(""); JLabel title = new JLabel("Order a pizza"); JComboBox sizeBox; JComboBox toppingBox; public JPizza() { ImageIcon pizzaPic = new ImageIcon("pizza.jpg"); JLabel pizzaLabel = new JLabel(pizzaPic); setTitle("Pizzaria"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,350); Container pane = getContentPane(); pane.setLayout(new FlowLayout()); sizeBox = new JComboBox(); String[] sizeArray = {"Small","Medium","Large","XLarge"}; sizeBox = new JComboBox(sizeArray); pane.add(sizeBox); String[] toppingArray = {"Cheese","Mushrooms","Pepperoni","Bacon","Chicken"}; toppingBox = new JComboBox(toppingArray); pane.add(toppingBox); pane.add(price); pane.add(title); pane.add(pizzaLabel); ClickButtonListener clickListener = new ClickButtonListener(); sizeBox.addActionListener(clickListener); toppingBox.addActionListener(clickListener); } public class ClickButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { int cost = 0; int topCost = 0; int sizeChoice = sizeBox.getSelectedIndex(); cost = sizeBox(sizeChoice); Object toppingChoice = toppingBox.getSelectedItem(); if(toppingChoice.equals("Cheese")) topCost = 1; cost += 1; price.setText("Your pizza costs $"+cost); } } } --------------------Configuration: <Default>-------------------- K:\CompSci\Projects\JPizza.java:64: error: cannot find symbol cost = sizeBox(sizeChoice); ^ symbol: method sizeBox(int) location: class JPizza.ClickButtonListener Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error Process completed.
change cost = sizeCost(sizeChoice); to cost = sizeBox(sizeChoice); or was it because i didn't declare sizeCost?
Where is the method: sizeCost() defined? The compiler can not find its definition.
The method is passed the index to the sizeBox combo box. Given that index, it could index into an array that contained the costs arranged in the same order as the words in the combo box.
sorry i have no idea how to define sizeCost and correlate it with my final ints.
would it be somewhere along the lines of
int sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
Code :import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JPizza extends JFrame { public static void main (String[] arg) { JPizza window = new JPizza(); window.setVisible(true); } final int SMALL = 7; final int MEDIUM = 9; final int LARGE = 11; final int XLARGE = 14; // final int topPrice = 1; JLabel price = new JLabel(""); JLabel title = new JLabel("Order a pizza"); JComboBox sizeBox; JComboBox toppingBox; public JPizza() { ImageIcon pizzaPic = new ImageIcon("pizza.jpg"); JLabel pizzaLabel = new JLabel(pizzaPic); setTitle("Pizzaria"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,350); Container pane = getContentPane(); pane.setLayout(new FlowLayout()); sizeBox = new JComboBox(); String[] sizeArray = {"Small","Medium","Large","XLarge"}; sizeBox = new JComboBox(sizeArray); pane.add(sizeBox); int sizeCost = {SMALL,MEDIUM,LARGE,XLARGE}; String[] toppingArray = {"Cheese","Mushrooms","Pepperoni","Bacon","Chicken"}; toppingBox = new JComboBox(toppingArray); pane.add(toppingBox); pane.add(price); pane.add(title); pane.add(pizzaLabel); ClickButtonListener clickListener = new ClickButtonListener(); sizeBox.addActionListener(clickListener); toppingBox.addActionListener(clickListener); } public class ClickButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { int cost = 0; int topCost = 0; int sizeChoice = sizeBox.getSelectedIndex(); cost = sizeCost(sizeChoice); Object toppingChoice = toppingBox.getSelectedItem(); if(toppingChoice.equals("Cheese")) topCost = 1; cost += 1; price.setText("Your pizza costs $"+cost); } } }
Very close. You have to make it an array by adding the []s:Code :int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
it still cannot find sizeCost
Code :import javax.swing.*; import java.awt.*; import java.awt.event.*; public class JPizza extends JFrame { public static void main (String[] arg) { JPizza window = new JPizza(); window.setVisible(true); } final int SMALL = 7; final int MEDIUM = 9; final int LARGE = 11; final int XLARGE = 14; // final int topPrice = 1; JLabel price = new JLabel(""); JLabel title = new JLabel("Order a pizza"); JComboBox sizeBox; JComboBox toppingBox; public JPizza() { ImageIcon pizzaPic = new ImageIcon("pizza.jpg"); JLabel pizzaLabel = new JLabel(pizzaPic); setTitle("Pizzaria"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(250,350); Container pane = getContentPane(); pane.setLayout(new FlowLayout()); sizeBox = new JComboBox(); String[] sizeArray = {"Small","Medium","Large","XLarge"}; sizeBox = new JComboBox(sizeArray); pane.add(sizeBox); int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE}; String[] toppingArray = {"Cheese","Mushrooms","Pepperoni","Bacon","Chicken"}; toppingBox = new JComboBox(toppingArray); pane.add(toppingBox); pane.add(price); pane.add(title); pane.add(pizzaLabel); ClickButtonListener clickListener = new ClickButtonListener(); sizeBox.addActionListener(clickListener); toppingBox.addActionListener(clickListener); } public class ClickButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { int cost = 0; int topCost = 0; int sizeChoice = sizeBox.getSelectedIndex(); cost = sizeCost(sizeChoice); Object toppingChoice = toppingBox.getSelectedItem(); if(toppingChoice.equals("Cheese")) topCost = 1; cost += 1; price.setText("Your pizza costs $"+cost); } } } --------------------Configuration: <Default>-------------------- K:\CompSci\Projects\JPizza.java:65: error: cannot find symbol cost = sizeCost(sizeChoice); ^ symbol: method sizeCost(int) location: class JPizza.ClickButtonListener Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error Process completed.
Does it exist anywhere?Quote:
it still cannot find sizeCost
Please post the full text of the error message that shows the source line where the error happens.
A method and an array are different things.
To index an array the syntax uses: []
To call a method the syntax uses: ()
the only place where i put sizeCost is
Code :int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
and
the array list is for the drop down option for the combobox.Code :cost = sizeCost(sizeChoice);
Please reread my last post #36.
error
Code :--------------------Configuration: <Default>-------------------- K:\CompSci\Projects\JPizza.java:65: error: cannot find symbol cost = sizeCost(sizeChoice); ^ symbol: method sizeCost(int) location: class JPizza.ClickButtonListener Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. 1 error Process completed.
Where is the method: sizeCost() defined? The compiler can not find it.Quote:
symbol: method sizeCost(int)
Code :public final int SMALL = 7; public final int MEDIUM = 9; public final int LARGE = 11; public final int XLARGE = 14;
Code :int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
Please read post #36 again about arrays and methods. That is a definition for an array, not for a method. The error message says there is a missing method.Code :int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
if i use int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE);
errors;
Code :--------------------Configuration: <Default>-------------------- K:\CompSci\Projects\JPizza.java:46: error: not a statement int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: ';' expected int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: ')' expected int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: not a statement int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: ';' expected int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: not a statement int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: ';' expected int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: not a statement int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ K:\CompSci\Projects\JPizza.java:46: error: ';' expected int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE); ^ 9 errors Process completed.
You had a choice of two places to change.
You changed one and it caused many errors, so that is not the right place to change.
Try changing the other place.
Read the tutorial about how to define an array:
Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
and how to define a method:
Defining Methods (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
and how to call a method:
Passing Information to a Method or a Constructor (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
i got it to work thank you
what does
meanCode :--------------------Configuration: <Default>-------------------- Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Process completed.
What line was that error on?
Use the javac command's -Xlint option to see what the problem is. My javac commandline from when I compile a program:
D:\Java\jdk1.6.0_29\bin\javac.exe -Xlint TestCode11.java
helloo! i am back with another program!
Code :import javax.swing.*; import java.awt.*; import java.awt.event.*; public class HedgeYourBet extends JFrame { public static void main (String[] arg) { HedgeYourBet window = new HedgeYourBet(); window.setVisible(true); } JLabel title = new JLabel("There is only 1 correct answer for each question."); JLabel one = new JLabel("If you choose 1 correct answer = 5 pts"); JLabel two = new JLabel("2 answers(with the 1 correct) = 2 pts"); JLabel three = new JLabel("all 3 answers = 1 pt"); JLabel direction = new JLabel("Choose the correct answer for x in each equation below"); String q1 = ("Q1) 1x + -1 = 0"); String q2 = ("Q2) 14x - 7 = 21"); String q3 = ("Q3) 5x + 5 = 10"); String q4 = ("Q4) x + 5 = 2x + 2"); String q5 = ("Q5) 3x - 5 = 2x - 4"); JLabel question = new JLabel(q1); JCheckBox q1a = new JCheckBox("x = 1"); JCheckBox q1b = new JCheckBox("x = 2"); JCheckBox q1c = new JCheckBox("x = 3"); JButton submit = new JButton("Submit"); int point = 0; JLabel points = new JLabel ("You have "+point+" points"); public HedgeYourBet() { setTitle("Quiz Game"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setSize(300,600); Container pane = getContentPane(); pane.setLayout(new FlowLayout());; pane.add(title); pane.add(one); pane.add(two); pane.add(three); pane.add(direction); pane.add(question); JPanel one = new JPanel(); one.add(q1a); one.add(q1b); one.add(q1c); JPanel submit2 = new JPanel(); submit2.add(submit); submit2.add(points); pane.add(one); pane.add(submit2); ClickButtonListener clickListener = new ClickButtonListener(); submit.addActionListener(clickListener); } public class ClickButtonListener implements ActionListener { public void actionPerformed (ActionEvent e) { if (question == q1) { if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected()) { point += 1; } else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() ) { point += 2; } else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected()) { point +=5; } } points.setText("You have "+point+" points"); question.setText(q2); if (question == q2) { if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected()) { point += 1; } else if(q1b.isSelected() && q1a.isSelected() && !q1c.isSelected() || q1b.isSelected() && q1c.isSelected() && !q1a.isSelected() ) { point += 2; } else if(q1b.isSelected() && !q1a.isSelected() && !q1c.isSelected()) { point +=5; } } points.setText("You have "+point+" points"); question.setText(q3); if (question == q3) { if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected()) { point += 1; } else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() ) { point += 2; } else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected()) { point +=5; } } points.setText("You have "+point+" points"); question.setText(q4); if (question == q4) { if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected()) { point += 1; } else if(q1c.isSelected() && q1b.isSelected() && !q1a.isSelected() || q1c.isSelected() && q1a.isSelected() && !q1b.isSelected() ) { point += 2; } else if(q1c.isSelected() && !q1b.isSelected() && !q1a.isSelected()) { point +=5; } } points.setText("You have "+point+" points"); question.setText(q5); if (question == q5) { if(q1c.isSelected() && q1b.isSelected() && q1a.isSelected()) { point += 1; } else if(q1a.isSelected() && q1b.isSelected() && !q1c.isSelected() || q1a.isSelected() && q1c.isSelected() && !q1b.isSelected() ) { point += 2; } else if(q1a.isSelected() && !q1b.isSelected() && !q1c.isSelected()) { point +=5; } } points.setText("You have "+point+" points"); if (point <= 10) { question.setText("Try Again!"); } else if (point >= 10) { question.setText("Congrats!"); } } } }
Code :--------------------Configuration: <Default>-------------------- K:\CompSci\Projects\HedgeYourBet.java:73: error: incomparable types: JLabel and String if (question == q1) ^ K:\CompSci\Projects\HedgeYourBet.java:92: error: incomparable types: JLabel and String if (question == q2) ^ K:\CompSci\Projects\HedgeYourBet.java:110: error: incomparable types: JLabel and String if (question == q3) ^ K:\CompSci\Projects\HedgeYourBet.java:129: error: incomparable types: JLabel and String if (question == q4) ^ K:\CompSci\Projects\HedgeYourBet.java:148: error: incomparable types: JLabel and String if (question == q5) ^ 5 errors Process completed.
how do i tell it to change to those ifs when the jlabel is at a specific string?
A JLabel is not a String and a String is not a JLabel. It does not make sense to test if they are equal.Quote:
how do i tell it to change to those ifs when the jlabel is at a specific string
Can you explain what the conditions in those if statements are supposed to be testing?
the program is little a mini quiz.
i put the questions as strings, q1,q2,q3 so when i click submit, the label will change from q1, to q2.
The answer choices are not going to chance.
However, the correct answer will change.
Therefore, I want the program to change the correct answer to the corresponding question.
So if the question is q1, the answer that will give 5 points is q1a.
Then, if the question is q2, the answer that will give 5 points is q1b.
If the question is q3, the answer that will give 5 points is q1c.
and so forth
How can the value of question (a reference to a JLabel) be the same as q1 (a reference to a String)?Quote:
if the question is q1,
The text displayed in a JLabel is a String. You could get that String and compare it to the other String.
Use the equals() method to compare the two Strings.