Using ArrayList

I'm having some problems implementing the ArrayList method correctly.  I don't know if I've written the code correctly but this is what I have:

import java.util.ArrayList;
    
    public class Dog implements Comparable <Dog>
   {
   // Instance variables
      private String dogName, dogBreed,  specialNeeds;
      private char dogGender;
      private int dogSize;
      private boolean specialNeedsInd;
      private double dogAge;
      private static int MINATURE = 1;
      private static int SMALL = 2;
      private static int MEDIUM = 3;
      private static int LARGE = 4;
      private static int GIANT = 5;
      
            
       public Dog(String Name, String Breed, String Gender, String SpecialNeeds, int Size, double Age )
      {
         this.dogName = dogName;
         this.dogBreed = dogBreed;
         this.dogGender = dogGender;
         this.specialNeeds = specialNeeds;
         this.dogSize = dogSize;
         this.dogAge = dogAge;
      }
      
   
   	
       public void setSize()//int Medium, int Minature, int Small, int Large, int Giant)
      {
         ArrayList arrayList = new ArrayList();
      
         arrayList.add(MINATURE);
         arrayList.add(SMALL);
         arrayList.add(MEDIUM);
         arrayList.add(LARGE);
         arrayList.add(GIANT);
      
      
         //MINATURE, SMALL, MEDIUM, LARGE, GIANT;
      }
         
       public int getSize()
      {      	
         return dogSize;
         
      }
      
         
       public void setAge(double a)
      {
      }
   
       public double getAge()
      {
         return dogAge;
      }
   	
   	   
       public void setName()
      {
         ArrayList arrayName = new ArrayList();
         
         arrayName.add("Molly");
         arrayName.add("Frankie");
         arrayName.add("Bella");
         arrayName.add("Poppy");
         arrayName.add("Pretzel");
         arrayName.add("Jenna");
         arrayName.add("Bob");
         arrayName.add("Bear");
         arrayName.add("");
         arrayName.add("");
         arrayName.add("Mr. T");
         arrayName.add("Java");
         arrayName.add("Nelly");
         arrayName.add("Lucy");
         arrayName.add("Sandy");
      
      }
   
       public String getName()
      {
         return dogName;
      }
      
       public boolean setSpecialNeeds(String Blind, String Deaf, String Neurological, String Amputee)
      {
         if(specialNeedsInd == true)
         {
         //specialNeeds = Blind || Deaf || Neurological || Amputee;
         // }
         //else (specialNeeds == false)
         
         //specialNeeds = "";
         
            return true;
         }
         else
         {
            return false;
         }
      }
   	
       public String getSpecialNeeds()
      {
         return "";
      }
       public void setBreed()
      {
         ArrayList arrayBreed = new ArrayList();
         arrayBreed.add("German Shepherd");
         arrayBreed.add("Dachshund");
         arrayBreed.add("Dachshund");
         arrayBreed.add("Beagle");
         arrayBreed.add("Pitbull");
         arrayBreed.add("Doberman");
         arrayBreed.add("Akita");
         arrayBreed.add("Labrador");
         arrayBreed.add("Akita");
         arrayBreed.add("Mix");
         arrayBreed.add("Bulldog");
         arrayBreed.add("Poodle");
         arrayBreed.add("Great Dane");
         arrayBreed.add("Bassett");
         arrayBreed.add("Mix");
      			 
      
      
      
      
      
      
      
      }
      
       public String getBreed()
      {
         return dogBreed;
      }
   
       public char getGender()
      {
         return dogGender;
      }
   
       public void setGender()
      {
         ArrayList arrayGender = new ArrayList();
         
      
      
      
      }
      
       public int compareTo(Dog d)
      {
         if(this.dogSize < d.getSize())
         {
            return 1;
         }
         else if(this.dogSize > d.getSize())
         {
            return -1;
         }
         else 
         {
            return 0;
         }
      }
      
       public String toString()
      {
         String output = ("");
         
         return output;
      }
   }

Open in new window

Slck_PAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mick BarryJava DeveloperCommented:
what is it you are trying to do exactly?
0
Slck_PAuthor Commented:
Set the methods equal to the action listener so that when the Size, Age, and Gender of the dog is selected a list of the dogs that fit that description will print in the JTextArea.  I was told to try and use ArrayList but I'm having trouble implementing it.  
0
Mick BarryJava DeveloperCommented:
you should use an ArrayList but not in your dog class
would be better placed in your gui class(es)
0
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Slck_PAuthor Commented:
I've been searching for code that will help me get started but I can't find any ArrayList with multiple entries for both row and column.  (I have 16 instances of different dogs with different information for each of them.  Any additional information is greatly appreciated.  
0
Mick BarryJava DeveloperCommented:
> but I can't find any ArrayList with multiple entries for both row and column

why do you need that?
sounds like you may need multiple lists
0
Slck_PAuthor Commented:
Here's a sample output I'm trying to get to:

Name: Bella    Breed: Pitbull   Age:  1.0   F(gender)  0(no special needs)

There are 16 additional dogs that I need to print out in that fashion according to the user input.  Could I accomplish this with just ArrayList?  Or would the multiple lists be more efficient?
0
Mick BarryJava DeveloperCommented:
you would keep a list of Dog's

List dogs = new ArrayList();

And as I mentioned above this would not go in your Dog class
0
Slck_PAuthor Commented:
Does this look like I'm on the right track?


import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

    public class PetAdoptionGUI extends JPanel
   {
   
      JTextArea dogOutput;
      JCheckBox specialNeeds;
      JRadioButton allButton;
      JRadioButton femaleButton;
      JRadioButton maleButton;
      JScrollPane scrollPane;
      JButton size;
      JButton age;
   
       public PetAdoptionGUI(ArrayList <Dog> dogs)
      {
         dogOutput = new JTextArea(7, 20);
         
         specialNeeds = new JCheckBox("Special Needs Only");
         allButton = new JRadioButton("All", true);
         femaleButton = new JRadioButton("Female Only", false);
         maleButton = new JRadioButton("Male Only", false);
      
         //all.setActionCommand();
         //all.setSelected(true);
      	
      	
         //femaleOnly.setActionCommand();
      	
      	
         //maleOnly.setActionCommand();
      	
      
      	
         
         
         ButtonGroup group = new ButtonGroup();
         group.add(allButton);
         group.add(femaleButton);
         group.add(maleButton);
         
         JPanel radioPanel = new JPanel();
         radioPanel.setLayout(new GridLayout(3, 1));
         radioPanel.add(allButton);
         radioPanel.add(femaleButton);
         radioPanel.add(maleButton);
         
      	
         size = new JButton("Size");
         age = new JButton("Age");
         
      	
         List <Dog> dogs = new ArrayList<Dog>();
      	
         dogs.add("Molly");
         dogs.add("Frankie");
         dogs.add("Bella");
         dogs.add("Poppy");
         dogs.add("Pretzel");
         dogs.add("Jenna");
         dogs.add("Bob");
         dogs.add("Bear");
         dogs.add("");
         dogs.add("");
         dogs.add("Mr. T");
         dogs.add("Java");
         dogs.add("Nelly");
         dogs.add("Lucy");
         dogs.add("Sandy");
      
        
         dogs.add(MINATURE);
         dogs.add(SMALL);
         dogs.add(MEDIUM);
         dogs.add(LARGE);
         dogs.add(GIANT);
         
      	
      
      	
      }
   }

Open in new window

0
Mick BarryJava DeveloperCommented:
What exactly are you going to use that dogs array for?
0
Slck_PAuthor Commented:
I'm just trying to make a list of the dogs with their information.  If I called the dogs array[0] I was under the impression it would return : Molly 5(miniature).  Is this incorrect thinking?
0
Mick BarryJava DeveloperCommented:
then you need to be adding Dog's to it (not strings)

dogs.add(new Dog(......
0
Slck_PAuthor Commented:
        dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));?

I'm using strings for the name and breed since they change each time.  
0
Mick BarryJava DeveloperCommented:
yes thats correct
0
Slck_PAuthor Commented:
In:
         List <Dog> dogs = new ArrayList<Dog>();

Should I set the parameters as the primitive values, variable names or leave empty?
ex:

new ArrayList<Dog>(String, String, int, char, int);
or
(dogName, dogBreed, dogAge, dogGender, specialNeeds);
or
()
0
Mick BarryJava DeveloperCommented:
leave empty
0
Slck_PAuthor Commented:
I'm getting a lot of missing symbol errors again.  Any help in pointing out any glaring errors is greatly appreciated.  

0
Slck_PAuthor Commented:
  import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;

    public class PetAdoptionGUI extends JPanel
   {
   
      JTextArea dogOutput;
      JCheckBox specialNeeds;
      JRadioButton allButton;
      JRadioButton femaleButton;
      JRadioButton maleButton;
      JScrollPane scrollPane;
      JButton size;
      JButton age;
   
       public PetAdoptionGUI(ArrayList <Dog> dogs)
      {
         dogOutput = new JTextArea(7, 20);
         
         specialNeeds = new JCheckBox("Special Needs Only");
         allButton = new JRadioButton("All", true);
         femaleButton = new JRadioButton("Female Only", false);
         maleButton = new JRadioButton("Male Only", false);
     
         //all.setActionCommand();
         //all.setSelected(true);
            
            
         //femaleOnly.setActionCommand();
            
            
         //maleOnly.setActionCommand();
            
     
            
         
         
         ButtonGroup group = new ButtonGroup();
         group.add(allButton);
         group.add(femaleButton);
         group.add(maleButton);
         
         JPanel radioPanel = new JPanel();
         radioPanel.setLayout(new GridLayout(3, 1));
         radioPanel.add(allButton);
         radioPanel.add(femaleButton);
         radioPanel.add(maleButton);
         
            
         size = new JButton("Size");
         age = new JButton("Age");
         
            
         List <Dog> dogs = new ArrayList<Dog>();
            
         dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));
         dogs.add(new Dog("Frankie", "Dachshund", SMALL, M, 5, 0));
         dogs.add(new Dog("Bella", "Dachshund", MINIATURE,F, 4, 1) );
         dogs.add(new Dog("Poppy", "Beagle", MEDIUM, F, 7, 1));
         dogs.add(new Dog("Pretzel","Pitbull", LARGE, F, 3, 0));
         dogs.add(new Dog("Jenna", "Doberman", LARGE, F, 5, 0));
         dogs.add(new Dog("Bob","Akita", GIANT, M, 2, 0 ));
         dogs.add(new Dog("Bear", "Labrador", LARGE, M, 1, 0));
         dogs.add(new Dog("", "Akita", GIANT, M, 0.5, 0));
         dogs.add(new Dog("", "Mix", SMALL, F, 0.3, 0));
         dogs.add(new Dog("Mr. T", "Bulldog", MEDIUM, M, 6,0));
         dogs.add(new Dog("Java", "Poodle", MEDIUM, M, 3, 1));
         dogs.add(new Dog("Nelly", "Great Dane", GIANT, F, 0.6, 0));
         dogs.add(new Dog("Lucy", "Bassett", MEDIUM, M, 1.3, 1));
         dogs.add(new Dog("Sandy", "Mix", MINIATURE, F, 6, 0));
     
       
         dogs.add(MINATURE);
         dogs.add(SMALL);
         dogs.add(MEDIUM);
         dogs.add(LARGE);
         dogs.add(GIANT);
         
            
     
            
      }
   }
0
Mick BarryJava DeveloperCommented:
get rid of these lines:

         dogs.add(MINATURE);
         dogs.add(SMALL);
         dogs.add(MEDIUM);
         dogs.add(LARGE);
         dogs.add(GIANT);

0
Arabia_vnCommented:
As far as i know,  dogs.add("Molly");  ---> will not compile. You can not a String to <Dog> list. And i really do not understand what you need. Normally you have to make a dog object by using it's constructor ( Name, Breed, String Gender, Size ...). You get the "Name" from a textbox, "Size" from a combobox (in your case since you have  MINATURE , SMALL , MEDIUM , LARGE , GIANT so you need to create an enum with 5 elements for your combox to display). You read all the  nessesary information for a dog from your textbox,combobox ... and forward those to your dog constructor to create a dog object and add to the list. If you want to display, simply read the dog object, assign it's property values (Name, Size...) to the corresponding Textbox, Combobox ...
0
Slck_PAuthor Commented:
I've removed those lines.  Here are the errors I'm getting:  (I  didn't put them all due to the large # of them)


PetAdoptionGUI.java:17: cannot find symbol
symbol  : class ArrayList
location: class PetAdoptionGUI
       public PetAdoptionGUI(ArrayList <Dog> dogs)
                             ^
PetAdoptionGUI.java:55: type java.awt.List does not take parameters
         List <Dog> dogs = new ArrayList<Dog>();
              ^
PetAdoptionGUI.java:55: cannot find symbol
symbol  : class ArrayList
location: class PetAdoptionGUI
         List <Dog> dogs = new ArrayList<Dog>();
                               ^
PetAdoptionGUI.java:57: cannot find symbol
symbol  : variable LARGE
location: class PetAdoptionGUI
         dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));
                                                      ^
PetAdoptionGUI.java:57: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));
                                                             ^
PetAdoptionGUI.java:58: cannot find symbol
symbol  : variable SMALL
location: class PetAdoptionGUI
         dogs.add(new Dog("Frankie", "Dachshund", SMALL, M, 5, 0));
                                                  ^
PetAdoptionGUI.java:58: cannot find symbol
symbol  : variable M
location: class PetAdoptionGUI
         dogs.add(new Dog("Frankie", "Dachshund", SMALL, M, 5, 0));
                                                         ^
PetAdoptionGUI.java:59: cannot find symbol
symbol  : variable MINIATURE
location: class PetAdoptionGUI
         dogs.add(new Dog("Bella", "Dachshund", MINIATURE,F, 4, 1) );
                                                ^
PetAdoptionGUI.java:59: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Bella", "Dachshund", MINIATURE,F, 4, 1) );
                                                          ^
PetAdoptionGUI.java:60: cannot find symbol
symbol  : variable MEDIUM
location: class PetAdoptionGUI
         dogs.add(new Dog("Poppy", "Beagle", MEDIUM, F, 7, 1));
                                             ^
PetAdoptionGUI.java:60: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Poppy", "Beagle", MEDIUM, F, 7, 1));
                                                     ^
PetAdoptionGUI.java:61: cannot find symbol
symbol  : variable LARGE
location: class PetAdoptionGUI
         dogs.add(new Dog("Pretzel","Pitbull", LARGE, F, 3, 0));
                                               ^
PetAdoptionGUI.java:61: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Pretzel","Pitbull", LARGE, F, 3, 0));
                                                      ^
PetAdoptionGUI.java:62: cannot find symbol
symbol  : variable LARGE
location: class PetAdoptionGUI
         dogs.add(new Dog("Jenna", "Doberman", LARGE, F, 5, 0));
                                               ^
PetAdoptionGUI.java:62: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Jenna", "Doberman", LARGE, F, 5, 0));

Open in new window

0
Mick BarryJava DeveloperCommented:
>          List  dogs = new ArrayList();

change that to:

         ArrayList  dogs = new ArrayList();

and add the import for ArrayList to the top of the file

import java.util.ArrayList;

>          dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));

change to:

         dogs.add(new Dog("Molly", "German Shepherd", Dog.LARGE, F, 2, 0));

(same for other Dogs you add

Will also need to make those vars public in your Dog class, eg.

      public static int LARGE = 4;
0
Slck_PAuthor Commented:
Sorry I can't tell what I'm supposed to change :

>          dogs.add(new Dog("Molly", "German Shepherd", LARGE, F, 2, 0));

to.  Did you switch the commas with periods?
0
Mick BarryJava DeveloperCommented:
changed LARGE to Dog.LARGE (so it know where to look for LARGE var)

though looking at it more closely, the args you pass don't match the constructor
need to match

       public Dog(String Name, String Breed, String Gender, String SpecialNeeds, int Size, double Age )

0
Slck_PAuthor Commented:
Here are the errors I'm getting now:

PetAdoptionGUI.java:58: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Molly", "German Shepherd", Dog.LARGE, F, 2, 0));
                                                                 ^
PetAdoptionGUI.java:58: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Molly", "German Shepherd", Dog.LARGE, F, 2, 0));
                  ^
PetAdoptionGUI.java:59: cannot find symbol
symbol  : variable M
location: class PetAdoptionGUI
         dogs.add(new Dog("Frankie", "Dachshund", Dog.SMALL, M, 5, 0));
                                                             ^
PetAdoptionGUI.java:59: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Frankie", "Dachshund", Dog.SMALL, M, 5, 0));
                  ^
PetAdoptionGUI.java:60: cannot find symbol
symbol  : variable MINIATURE
location: class Dog
         dogs.add(new Dog("Bella", "Dachshund",Dog.MINIATURE,F, 4, 1) );
                                                  ^
PetAdoptionGUI.java:60: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Bella", "Dachshund",Dog.MINIATURE,F, 4, 1) );
                                                             ^
PetAdoptionGUI.java:60: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Bella", "Dachshund",Dog.MINIATURE,F, 4, 1) );
                  ^
PetAdoptionGUI.java:61: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Poppy", "Beagle", Dog.MEDIUM, F, 7, 1));
                                                         ^
PetAdoptionGUI.java:61: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Poppy", "Beagle", Dog.MEDIUM, F, 7, 1));
                  ^
PetAdoptionGUI.java:62: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Pretzel","Pitbull", Dog.LARGE, F, 3, 0));
                                                          ^
PetAdoptionGUI.java:62: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Pretzel","Pitbull", Dog.LARGE, F, 3, 0));
                  ^
PetAdoptionGUI.java:63: cannot find symbol
symbol  : variable F
location: class PetAdoptionGUI
         dogs.add(new Dog("Jenna", "Doberman", Dog.LARGE, F, 5, 0));
                                                          ^
PetAdoptionGUI.java:63: internal error; cannot instantiate Dog.<init> at Dog to ()
         dogs.add(new Dog("Jenna", "Doberman", Dog.LARGE, F, 5, 0));
                  ^


Here's my new code:

and here's the new constructor:

       public Dog(String Name, String Breed, int Size, char Gender, double Age, int SpecialNeeds, String specialDescription )


I've never gotten the internal error before.  

import java.awt.*;
   import java.awt.event.*;
   import javax.swing.*;
   import java.util.ArrayList;

    public class PetAdoptionGUI extends JPanel
   {
   
      JTextArea dogOutput;
      JCheckBox specialNeeds;
      JRadioButton allButton;
      JRadioButton femaleButton;
      JRadioButton maleButton;
      JScrollPane scrollPane;
      JButton size;
      JButton age;
   
       public PetAdoptionGUI(ArrayList <Dog> dogs)
      {
         dogOutput = new JTextArea(7, 20);
         
         specialNeeds = new JCheckBox("Special Needs Only");
         allButton = new JRadioButton("All", true);
         femaleButton = new JRadioButton("Female Only", false);
         maleButton = new JRadioButton("Male Only", false);
      
         //all.setActionCommand();
         //all.setSelected(true);
      	
      	
         //femaleOnly.setActionCommand();
      	
      	
         //maleOnly.setActionCommand();
      	
      
      	
         
         
         ButtonGroup group = new ButtonGroup();
         group.add(allButton);
         group.add(femaleButton);
         group.add(maleButton);
         
         JPanel radioPanel = new JPanel();
         radioPanel.setLayout(new GridLayout(3, 1));
         radioPanel.add(allButton);
         radioPanel.add(femaleButton);
         radioPanel.add(maleButton);
         
      	
         size = new JButton("Size");
         age = new JButton("Age");
         
      	
         ArrayList <Dog> dogs = new ArrayList<Dog>();
      	
         dogs.add(new Dog("Molly", "German Shepherd", Dog.LARGE, F, 2, 0));
         dogs.add(new Dog("Frankie", "Dachshund", Dog.SMALL, M, 5, 0));
         dogs.add(new Dog("Bella", "Dachshund",Dog.MINIATURE,F, 4, 1) );
         dogs.add(new Dog("Poppy", "Beagle", Dog.MEDIUM, F, 7, 1));
         dogs.add(new Dog("Pretzel","Pitbull", Dog.LARGE, F, 3, 0));
         dogs.add(new Dog("Jenna", "Doberman", Dog.LARGE, F, 5, 0));
         dogs.add(new Dog("Bob","Akita", Dog.GIANT, M, 2, 0 ));
         dogs.add(new Dog("Bear", "Labrador", Dog.LARGE, M, 1, 0));
         dogs.add(new Dog("", "Akita", Dog.GIANT, M, 0.5, 0));
         dogs.add(new Dog("", "Mix", Dog.SMALL, F, 0.3, 0));
         dogs.add(new Dog("Mr. T", "Bulldog", Dog.MEDIUM, M, 6,0));
         dogs.add(new Dog("Java", "Poodle", Dog.MEDIUM, M, 3, 1));
         dogs.add(new Dog("Nelly", "Great Dane", Dog.GIANT, F, 0.6, 0));
         dogs.add(new Dog("Lucy", "Bassett", Dog.MEDIUM, M, 1.3, 1));
         dogs.add(new Dog("Sandy", "Mix", Dog.MINIATURE, F, 6, 0));
      
      	
      
      	
      }
   }

Open in new window

0
Mick BarryJava DeveloperCommented:
F should be 'F'
M should be 'M'
0
Slck_PAuthor Commented:
Thanks for all the help.  I've gotten the errors down to 2 now.  The errors exist on all of the
.add
and all of the
new (new Dog)
0
Mick BarryJava DeveloperCommented:
we're drifting away from the original q
post the latest errors in a new question and we'll have a look at it 4u
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slck_PAuthor Commented:
Excellent support and help.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Editors IDEs

From novice to tech pro — start learning today.