Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 60

Thread: Beginner help please.

  1. #26
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    Where are they defined? Are they in scope (within the same pair of {}s) as where they are being referenced?
    If you don't understand my answer, don't ignore it, ask a question.

  2. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 13th, 2012)

  3. #27
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    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;

  4. #28
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    Is the program working now?
    If you don't understand my answer, don't ignore it, ask a question.

  5. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  6. #29
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    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?

    ***********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.

  7. #30
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    how do i add the cost of the pizza's size into my output
    Where is the pizza's size determined? How can/does the program get the size?
    When it gets the size, how does it get the cost?
    Where is the output that the cost should be added to?
    If you don't understand my answer, don't ignore it, ask a question.

  8. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  9. #31
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    Quote Originally Posted by Norm View Post
    Where is the pizza's size determined?
    final int SMALL = 7;
    final int MEDIUM = 9;
    final int LARGE = 11;
    final int XLARGE = 14;


    How can/does the program get the size?
    String[] sizeArray = {"Small","Medium","Large","XLarge"};
    sizeBox = new JComboBox(sizeArray);


    When it gets the size, how does it get the cost?
    int sizeChoice = sizeBox.getSelectedIndex();
    cost = sizeBox(sizeChoice);


    Where is the output that the cost should be added to?
    if(toppingChoice.equals("Cheese"))
    topCost = 1;
    cost += 1;
    price.setText("Your pizza costs $"+cost);
    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?

  10. #32
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  12. #33
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    Quote Originally Posted by Norm View Post
    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};


    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);
            }
        }
     
    }

  13. #34
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    Very close. You have to make it an array by adding the []s:
    int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};
    If you don't understand my answer, don't ignore it, ask a question.

  14. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  15. #35
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    it still cannot find sizeCost

    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.

  16. #36
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    it still cannot find sizeCost
    Does it exist anywhere?

    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: ()
    If you don't understand my answer, don't ignore it, ask a question.

  17. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  18. #37
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    the only place where i put sizeCost is

        int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};

    and

     	cost = sizeCost(sizeChoice);
    the array list is for the drop down option for the combobox.

  19. #38
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    Please reread my last post #36.
    If you don't understand my answer, don't ignore it, ask a question.

  20. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  21. #39
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    error

    --------------------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.

  22. #40
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    symbol: method sizeCost(int)
    Where is the method: sizeCost() defined? The compiler can not find it.
    If you don't understand my answer, don't ignore it, ask a question.

  23. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  24. #41
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    public final int SMALL = 7;
    	public final int MEDIUM = 9;
    	public final int LARGE = 11;
    	public final int XLARGE = 14;
    int[] sizeCost = {SMALL,MEDIUM,LARGE,XLARGE};

  25. #42
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  26. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  27. #43
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    if i use int() sizeCost = (SMALL,MEDIUM,LARGE,XLARGE);

    errors;
    --------------------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.

  28. #44
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    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)
    If you don't understand my answer, don't ignore it, ask a question.

  29. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  30. #45
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    i got it to work thank you

    what does

    --------------------Configuration: <Default>--------------------
    Note: K:\CompSci\Projects\JPizza.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
     
    Process completed.
    mean

  31. #46
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    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
    If you don't understand my answer, don't ignore it, ask a question.

  32. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 16th, 2012)

  33. #47
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    helloo! i am back with another program!
    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!");
    			 }
     
            }
        }
    }

    --------------------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?

  34. #48
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    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.

    Can you explain what the conditions in those if statements are supposed to be testing?
    If you don't understand my answer, don't ignore it, ask a question.

  35. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 18th, 2012)

  36. #49
    Member
    Join Date
    Dec 2012
    Posts
    30
    Thanks
    29
    Thanked 0 Times in 0 Posts

    Default Re: Beginner help please.

    Quote Originally Posted by Norm View Post
    A JLabel is not a String and a String is not a JLabel. It does not make sense to test if they are equal.

    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

  37. #50
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,437
    Thanks
    49
    Thanked 2,217 Times in 2,190 Posts

    Default Re: Beginner help please.

    if the question is q1,
    How can the value of question (a reference to a JLabel) be the same as q1 (a reference to a String)?

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  38. The Following User Says Thank You to Norm For This Useful Post:

    xdorkiee (December 18th, 2012)

Page 2 of 3 FirstFirst 123 LastLast

Similar Threads

  1. Please help me I'm a beginner
    By alloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 14th, 2012, 03:23 AM
  2. I am a beginner please help :)
    By mvonb17 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 6th, 2012, 02:44 PM
  3. Beginner
    By codejava in forum Member Introductions
    Replies: 2
    Last Post: August 22nd, 2011, 08:11 AM
  4. Beginner
    By angelo24 in forum Member Introductions
    Replies: 1
    Last Post: August 19th, 2011, 07:14 AM
  5. I need a help ! i am beginner
    By yinky in forum Java Theory & Questions
    Replies: 3
    Last Post: September 30th, 2009, 07:22 AM