Counting occurrence of attributes inside the objects of a Java Array

139
February 11, 2017, at 11:39 AM

I have created an array of 25 Flower objects. Each flower object holds the flower name(String), flower color(String), presence of thorns(boolean), and flower scent(String). These attributes are handled by the 'Flower' class. I have pasted both classes in case the error is being caused by either class. The user inputs all of the attributes of the flowers when the menu prompts for the information. After the user enters all of the flowers they want to, I need to be able to print out the entire array and a counter of how many of each flower there are. For instance, if the user puts in 10 flowers and there are 3 Roses, 2 Lilly's, 3 Dandelions, and 2 Orchids, I need to print the entire array and then print the number each flower was present. The format for the display is:

Flower name: Rose Flower color: Red Flower has thorns: true Flower scent: Sweet Rose - 3 Lilly - 3 Dandelion - 3 Orchid - 2

I am able to print out the array as shown, but cannot get the count variable to work properly. I do not need to sort this array.

Another issue I am getting in an OutOfBounds error. I can only put in 24 flowers before I encounter this error. The 25th flower triggers it. I thought this was covered by the addFlower index counter, but obviously, I was incorrect.

This assignment does not allow the use of ArrayList, which would make this much simpler. We have not explored error handling yet either.

Current code is:

package assignment2;
import java.util.Scanner;
public class Assignment2 
{
    public static void main(String[] args) 
    {
        new Assignment2();
    }
    public Assignment2() 
    {
        Scanner input = new Scanner(System.in);
        Flower flowerPack[] = new Flower[25];
        System.out.println("Welcome to my flower pack interface.");
        System.out.println("Please select a number from the options below");
        System.out.println("");
        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!");
                input.close();
                System.exit(0);             
            }
        }   
    }

    private void addFlower(Flower flowerPack[]) 
    {       
        String flowerName; // Type of flower
        String flowerColor; // Color of the flower
        Boolean hasThorns = false; // Have thorns?
        String flowerScent;   // Smell of the flower
        int index = 0;
        Scanner input = new Scanner(System.in);
        System.out.println("What is the name of flower is it?");
        flowerName = input.nextLine();
        System.out.println("What color is the flower?");
        flowerColor = input.nextLine();
        System.out.println("Does the flower have thorns?");
        System.out.println("Choose 1 for yes, 2 for no");
        int thorns = input.nextInt();
        if(thorns == 1)
        {
            hasThorns = true;
        }
        input.nextLine();
        System.out.println("What scent does the flower have?");
        flowerScent = input.nextLine();        
        Flower fl1 = new Flower(flowerName, flowerColor, hasThorns, flowerScent);  
        for(int i = 0; i < flowerPack.length; i++)
        {
            if(flowerPack[i] != null)
            {
                index++;            
                if(index == flowerPack.length)
                {
                    System.out.println("The pack is full");
                }               
            }
            else
            {
                flowerPack[i] = fl1;  
                break;
            }                       
        }
    }
    private void removeFlower(Flower flowerPack[]) 
    {       
        Scanner input = new Scanner(System.in);
        System.out.println("What student do you want to remove?");
        displayFlowers(flowerPack);
        System.out.println("Choose 1 for the first flower, 2 for the second, etc" );
        int index = input.nextInt();
        index = index - 1;
        for (int i = 0; i < flowerPack.length - 1; i++) 
        {
            if(flowerPack[i] != null && flowerPack[i].equals(flowerPack[index]))
            {               
                flowerPack[i] = flowerPack[i + 1];                
            }
        } 
    }
    private void searchFlowers(Flower flowerPack[]) 
    {
        Scanner input = new Scanner(System.in);
        String name;
        System.out.println("What flower would you like to search for?");
        name = input.nextLine();     
        boolean found = false;
        for (int i = 0; i < flowerPack.length; i++) 
        {
            if (flowerPack[i].getFlowerName().equalsIgnoreCase(name)) 
            {
                found = true;
                break;
            }
        }
        if (found) 
        {
            System.out.println("We found your flower.");
        } 
        else 
        {
            System.out.println("That flower was not found.");
        }        
    }
    private void displayFlowers(Flower flowerPack[])
    {
        int count = 1;
        for(int i = 0; i < flowerPack.length; i++)
        {
            if (flowerPack[i] != null)
            {           
                if (flowerPack[i].equals(flowerPack[i+1]))
                {               
                    count++;
                } 
                else
                {
                    System.out.println(flowerPack[i]);
                    count = 1;
                }
            }
            else
            {
                if (flowerPack[i] == null)
                {
                    break;  
                }
            }
        }           
    }   
}

The Flower class is below. package assignment2;

public class Flower 
{   
    @Override
    public String toString() 
    {
        return "Flower name: " + this.getFlowerName() +  "\t" +          
               "Flower color: " + this.getFlowerColor() + "\t" +
               "Flower has thorns: " + this.getHasThorns() + "\t" +
               "Flower scent: " + this.getFlowerScent() + "\t" ;
    }   
    private String flowerName;
    private String flowerColor;
    private Boolean hasThorns;
    private String flowerScent;
    Flower(String flowerName, String flowerColor, Boolean hasThorns, String flowerScent) 
    {
        this.flowerName = flowerName;
        this.flowerColor = flowerColor;
        this.hasThorns = hasThorns;
        this.flowerScent = flowerScent;
    }   
    String getFlowerName() 
    {
        return flowerName;
    }
    private void setFlowerName(String flowerName)
    {
        this.flowerName = flowerName;
    }
    private String getFlowerColor() 
    {
        return flowerColor;
    }
    private void setFlowerColor()
    {
        this.flowerColor = flowerColor;
    }
    private Boolean getHasThorns() 
    {
        return hasThorns;
    }
    private void setHasThorns()
    {
        this.hasThorns = hasThorns;
    }
    private String getFlowerScent() 
    {
        return flowerScent;
    }
    private void setFlowerScent()
    {
        this.flowerScent = flowerScent;
    }
}
READ ALSO
Java not creating file

Java not creating file

I copied this code from my book Introduction to Java Programming, and it compiles correctly but it doesn't create the file, and returns false and zero bytes for the methodsCan someone help please I will give best answer

148
Java string to date conversion

Java string to date conversion

Can somebody recommend the best way to convert a string in the format 'January 2, 2010' to a date in java? Ultimately, I want to break out the month, the day, and the year as integers so that I can use:

118
In Java can you limit the input to where only a number can be entered or only a certain amount of numbers can be entered?

In Java can you limit the input to where only a number can be entered or only a certain amount of numbers can be entered?

In Java can you limit the input using the scanner to where only a certain types such as numbers or letters can be enteredAlso can you limit input to a certain amount of characters that can be entered?

101
Android Studio 2.2.3 - Import the DecimalFormat class Java Code

Android Studio 2.2.3 - Import the DecimalFormat class Java Code

I am quite confused when I get this error within my MainActivityjava code inside Android Studio

157