Results 1 to 5 of 5
  1. #1
    SmokeyWoody is offline Member
    Join Date
    Oct 2014
    Posts
    36
    Rep Power
    0

    Default Adding to a non-static array from a main method

    Honestly stuck with what I have to do here. I believe I am on the right track but when I run the program I get an error that the class that I can't call on the method. I need to have what is selected in the JCheckBoxes added to the toppings array and I can't get that done what so ever. Any help will be greatly appreciated.

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    
    public class PizzaOrderListener
    {
       public static void main(String[] args)        
       {  
    
          JFrame frame = new JFrame();
          JPanel panel = new JPanel();
          frame.add(panel); 
          panel.setBackground(Color.WHITE);
          
          JTextField nameInput;
          nameInput = new JTextField(30);
          panel.add(nameInput);
          
          JComboBox size = new JComboBox();
          size.addItem("Select a size");
          size.addItem("10 Inch pizza - $10.79");
          size.addItem("12 Inch pizza - $12.79");
          size.addItem("14 Inch pizza - $14.79");
          size.addItem("16 inch pizza - $16.79");
          panel.add(size);
          
          JComboBox crust = new JComboBox();
          crust.addItem("Select a crust");
          crust.addItem("Hand tossed");
          crust.addItem("Deep dish");
          crust.addItem("Thin Crust");
          panel.add(crust);
          
          JCheckBox pepperoni = new JCheckBox("Pepperoni");
          JCheckBox sausage = new JCheckBox("Sausage");
          JCheckBox bacon = new JCheckBox("Bacon");
          JCheckBox mushroom = new JCheckBox("Mushroom");
          JCheckBox cheesey = new JCheckBox("Extra Cheese");
          panel.add(pepperoni);
          panel.add(sausage);
          panel.add(bacon);
          panel.add(mushroom);
          panel.add(cheesey);
          
          
          //Button to submit.    
          JButton button = new JButton("Submit");
          panel.add(button);
          JPanel southPanel = new JPanel();
          final JLabel label = new JLabel("Please create your pizza.");
          southPanel.add(label);
          frame.add(label, BorderLayout.SOUTH);
          
          //Button to clear.
          JButton button2 = new JButton("Clear");
          panel.add(button2);
                
          //PizzaOrder pizza = new PizzaOrder();
          ArrayList<String> toppings = new ArrayList<String> toppings();
          
          class ClickListener implements ActionListener 
          {
             public void actionPerformed(ActionEvent event)   
             {  
                if(pepperoni.isSelected());
                   toppings.addTopping("Pepperoni");
                   
                if(sausage.isSelected());
                   pizza.addTopping("Sausage");
                   
                if(bacon.isSelected());
                   pizza.addTopping("Bacon");
                   
                if(mushroom.isSelected());
                   pizza.addTopping("Mushroom");
                   
                if(cheesey.isSelected());
                   pizza.addTopping("Extra Cheese");
               
             }            
          }      
          ActionListener listener = new ClickListener();
          button.addActionListener(listener);
          frame.setSize(400, 200);
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setVisible(true);  
       }
    }
    Java Code:
    import java.util.ArrayList;
    import java.awt.event.*;
    import javax.swing.*;
    import java.awt.*;
    import javax.swing.JMenu; 
    import javax.swing.JMenuBar; 
    import javax.swing.JMenuItem;
    
    public class PizzaOrder
    {
       private int inch;
       private String crustType;
       private static ArrayList<String> toppings;
       private String name; 
       private int price;
    
       public PizzaOrder(int size, String crustType, String name)
       {
          
        
       }
       
       public void addTopping (String topping)
       {
          PizzaOrderListener.addTopping
       }
       
       private double calculatePrice()
       {
          
          return price;
       }
       
       public String toString()
       {
          return crustType;
       }  
    }

  2. #2
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Adding to a non-static array from a main method

    Line 59 contains an error:
    Java Code:
    ArrayList<String> toppings = new ArrayList<String> toppings();
    should be
    Java Code:
    ArrayList<String> toppings = new ArrayList<String>();
    You are calling the constructor of the ArrayList class, not the toppings class. "toppings" is just a variable pointing to that ArrayList.

    Then on line 66 you call addTopping(...) but an ArrayList doesn't not have a method called that. There is a method add(...) though.

    Then on line 69, 72, 75 and 78 you use a variable "pizza". You never create one, so the compiler has no idea what to do here.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Adding to a non-static array from a main method

    Well, I will say you're apparently supposed to be creating a PizzaOrder, which has an addTopping() method, so I expect that's what you should be using instead of an ArrayList in your main() method.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    1,993
    Rep Power
    9

    Default Re: Adding to a non-static array from a main method

    Quote Originally Posted by Tolls View Post
    Well, I will say you're apparently supposed to be creating a PizzaOrder, which has an addTopping() method, so I expect that's what you should be using instead of an ArrayList in your main() method.
    You should probably have both: A List that contains PizzaOrders.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Adding to a non-static array from a main method

    I recommend that you write a small test app which allows you to add items to an array list. Just use keyboard input for this via Scanner or have some canned values. Then you can add items to a Map (perhaps HashMap) of arrayLists. You can then try to convert that to a GUI using checkboxes. The point is (and I do this all the time), when you are not certain how to do something or are unfamiliar with the JDK then remove it from the the clutter of your main applications and experiment with it. It becomes easier to do without the added distraction of the all the other code you need to work with.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 4
    Last Post: 03-28-2013, 09:40 PM
  2. Error when adding an array into main method
    By abeidson in forum New To Java
    Replies: 7
    Last Post: 03-12-2013, 05:49 PM
  3. Replies: 5
    Last Post: 09-17-2012, 07:47 AM
  4. Replies: 2
    Last Post: 09-27-2011, 04:59 PM
  5. Why did Java designers make main method as static ?
    By makpandian in forum New To Java
    Replies: 9
    Last Post: 11-13-2010, 10:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •