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.

Results 1 to 4 of 4

Thread: Object not storeing in array?

  1. #1
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Object not storeing in array?

    Here is my code

     
    import java.util.Scanner;
     
    public class Assignment2 {
        public static void main(String[] args) {
            new Assignment2();
        }
     
        // This will act as our program switchboard
        public Assignment2() {
     
    		Flower[] flowerPack = new Flower[25];
     
     
            Scanner input = new Scanner(System.in);
     
            while (true) {
                // Give the user a list of their options
                System.out.println("1: Add an item to the pack.");
                System.out.println("2: Remove an item from the pack.");
                System.out.println("3: Search for a flower.");
                System.out.println("4: Display the flowers in the pack.");
                System.out.println("0: Exit the flower pack interfact.");
     
                // Get the user input
                int userChoice = input.nextInt();
     
                switch (userChoice) {
                case 1:
                    addFlower(flowerPack);
                    break;
                case 2:
                    removeFlower(flowerPack);
                    break;
                case 3:
                    searchFlowers(flowerPack);
                    break;
                case 4:
                    displayFlowers(flowerPack);
                    break;
                case 0:
                    System.out
                            .println("Thank you for using the flower pack interface. See you again soon!");
                    System.exit(0);
                }
            }
     
     
     
     
        }
     
        private void addFlower(Flower flowerPack[])  
        {
         Scanner scan = new Scanner(System.in);
         Flower flowerClass = new Flower();
     
         System.out.println();
     
         System.out.println( " Please enter your flower type? i.e. type of flower, color, presence of thorns, smell " );
         System.out.println( " Please be mindful that you may only insert 25 flower types. " );
         System.out.print( " Type of flower? : ");
         flowerClass.setFlower(scan.next());
         System.out.print( " Color? : ");
         flowerClass.setColorFlower(scan.next());
         System.out.print( " Thorns Presented? : ");
         flowerClass.setThornAvailable(scan.next());
         System.out.print( " Smell? :  ");
         flowerClass.setFlowerSmell(scan.next());
     
     
         for(int i = 0; i < flowerPack.length; i++)
         {
             if(flowerPack[i] == null)
             {
     
             flowerPack[i] = new Flower();
             break;
             }
     
     
     
         }
     
     
         for(int x = 0; x < flowerPack.length; x++)
     
         if( (x + 1) % 5 == 0)    
         { 
         System.out.println(" " + flowerPack[x] + " ");
         } 
         else
         {
          System.out.print( " " + flowerPack[x] + " ");
     
         }
     
     
     
     
     
     
                }
     
        private void removeFlower(Flower flowerPack[]) {
     
     
            Scanner scan = new Scanner(System.in);
            System.out.print( " Please enter a flower you would like to remove. ");
            String deleteflower = scan.nextLine();
            int i;
     
            for(i = 0; i < flowerPack.length; i++)
            {
               if( deleteflower.equals(flowerPack[i]) )
                {
                flowerPack[i] = null;
     
               }
     
            }
     
    }      
     
     
     
     
     
     
        private void searchFlowers(Flower flowerPack[]) {
                    Scanner scan = new Scanner(System.in);
     
            System.out.print( " Please enter the flower you would like to search for.");
            String key = scan.next();
     
          boolean found = false;
     
        for(int y = 0; y < flowerPack.length; y++)
        {
     
            if( key.equals(flowerPack[y]) )
            {
                found = true;
            }   
        }
     
         if(found)
          {
          System.out.println( key +  " is in the batch0 of flowers.");
          System.out.println("");
          }
     
         else   
          {
          System.out.println(" No results found.");
          System.out.println("");
          }   
     
     
     
    }
     
    private void displayFlowers(Flower flowerPack[]) 
    {
     
     
    }
     
    }
     
     
     
    //Here is my class
     
     
    public class Flower
    {
        String flowerName;
        String colorFlower;
        String thornAvailable;
        String flowerSmell;
     
        Flower(){
     
        }
     
     
        public void setFlower(String flower)
        {
            flowerName = flower;
        }
     
        public void setColorFlower(String color)
        {
            colorFlower = color;
        }
     
        public void setThornAvailable(String thorn)
        {
            thornAvailable = thorn;
        }
     
        public void setFlowerSmell(String smell )
        {
           flowerSmell= smell;
        }
     
     
     
        public String getFlower()
        {
           return flowerName; 
        }
     
        public String getcolor()
        {
            return colorFlower;
        }
     
        public String getthorn()
        {
            return thornAvailable;
        }
     
        public String getsmell()
        {
            return flowerSmell;
        }
     
     
    }


    Here is my output.

    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.
    1

    Please enter your flower type? i.e. type of flower, color, presence of thorns, smell
    Please be mindful that you may only insert 25 flower types.
    Type of flower? : rose
    Color? : rose
    Thorns Presented? : yes
    Smell? : no
    Flower@35bab2 null null null null
    null null null null null
    null null null null null
    null null null null null
    null null null null null
    1: Add an item to the pack.
    2: Remove an item from the pack.
    3: Search for a flower.
    4: Display the flowers in the pack.
    0: Exit the flower pack interfact.



    I'm been working on this for awhile. I'm so tried I must have over looked something. I know it something easy. Can someone help me?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Object not storeing in array?

    What should the output look like?

    What variables are being printed as null?
    Where is the code that is supposed to assign those variables non-null values? Why isn't that code being executed?

    What is done with the instance of Flower: flowerClass after it has been given the user's input?

    --- Update ---

    What is in the flowerPack array?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Location
    Detroit Mi
    Posts
    125
    My Mood
    Nerdy
    Thanks
    14
    Thanked 0 Times in 0 Posts

    Default Re: Object not storeing in array?

    I will try to answer the questions as best as I can.

    What should the output look like? My output is supposed to have an object of my Flower class. The flower class consists of attributes of my flower class. i.e. flowertype, flowercolor, thronsAvailable and flowersmell. Output should look like this.

    Rose Red Yes Yes
    Daisy, Blue, No, Yes
    etc
    etc

    What variables are being printed as null? Your guess is mines. I think it has something to do with my Flower constructor?

    Where is the code that is supposed to assign those variables non-null values?

    Any null variable should have my flower object stored. Which is displayed below.

    for(i = 0; i < flowerPack.length; i++)
    {
    if( deleteflower.equals(flowerPack[i]) )
    {
    flowerPack[i] = null;

    }

    }


    Why isn't that code being executed? The code is being executed but its assign to some type of weird character combination i.e. FlowerPack[1] = Flower@35bab2.

    What is in the flowerPack array? The flower pack has a default value of null.

    I hope this helps answer your question.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,252
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: Object not storeing in array?

    I've asked those questions about areas of the code that you need to look at.

    Starting with the print/println statements that print out the variables with null values, you need to back track in the code to see why those variables do not have non-null values.

    If you put values into instance A of a class and print the contents of instance B, the values that were put into instance A will not be printed. That's what your code does. Look at the flowerClass instance and look at the instances of Flower in the flowerPack array. You put values in the first and print what is in the second.

    --- Update ---

    I've asked those questions about areas of the code that you need to look at.

    Starting with the print/println statements that print out the variables with null values, you need to back track in the code to see why those variables do not have non-null values.

    If you put values into instance A of a class and print the contents of instance B, the values that were put into instance A will not be printed. That's what your code does. Look at the flowerClass instance and look at the instances of Flower in the flowerPack array. You put values in the first and print what is in the second.

    --- Update ---

    Flower@35bab2.
    That is the String returned by the Object class's toString() method. It has the class name @ and a hex number. If you want to see something different where that String appears, override the Flower class's toString() method and return the String that you want to see.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. storeing 2d array in arraylist?
    By game06 in forum What's Wrong With My Code?
    Replies: 22
    Last Post: March 24th, 2013, 12:27 PM
  2. Array of object
    By maple1100 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 20th, 2013, 10:13 AM
  3. Reading from ResultSet to Object and from object Object Array
    By anmaston in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 7th, 2011, 07:11 AM
  4. Array object and constructors
    By TarunN in forum Collections and Generics
    Replies: 14
    Last Post: May 6th, 2010, 05:04 PM
  5. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 05:09 AM