Linuxn00b Linuxn00b - 1 month ago 21
Java Question

Running into a bit of a conundrum with my program (note can not use ArrayList)

my program consists of a String Array with a size of 25. What I'm attempting to do is create an add method that adds elements to this array When I attempt to add the same string, I create a method that displays the number of times that particular item is listed within the String array (I use a hash map to do this). The program is not working as planned. There seems to be something off about my add method because when I add a new element, instead of incrementing to the next index in the array, it restarts at index 0 (which makes sense since each time you call the add method i = 0). What can I do in order to modify this method to go to the next index if something is added? Thanks, please see code below:

import java.util.*;

public class Assignment1 {




public static void main(String[] args){
new Assignment1 ();
}

// This will act as our program switchboard
public Assignment1 (){
Scanner input = new Scanner(System.in);
String[] flowerPack = new String[25];
int[] flowerCount = new int[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: Sort the contents of the pack.");
System.out.println("4: Search for a flower.");
System.out.println("5: 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:
sortFlowers(flowerPack);
break;
case 4:
searchFlowers(flowerPack);
break;
case 5:
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(String flowerPack[]) {
// TODO: Add a flower that is specified by the user
int i = 0;

String flowerName;
Scanner flowerInput = new Scanner(System.in);

System.out.println("Please enter the name of a flower type to add:");
flowerName = flowerInput.nextLine();
flowerPack[i] = flowerName;
i++;

}

private void removeFlower(String flowerPack[]) {
// TODO: Remove a flower that is specified by the user
String flowerName;
Scanner flowerInput = new Scanner(System.in);
System.out.println("Please enter the name of a flower type to remove");
flowerName = flowerInput.nextLine();

for (int i = 0; i < flowerPack.length; i++) {
if (flowerPack[i].equals(flowerName)) {
flowerPack[i] = ""; //Will ask professor if we should set to NULL instead
}
}
}

private void sortFlowers(String flowerPack[]) {
// TODO: Sort the flowers in the pack (No need to display them here) - Use Selection or Insertion sorts
// NOTE: Special care is needed when dealing with strings! research the compareTo() method with strings

Arrays.sort(flowerPack);
}

private void searchFlowers(String flowerPack[]) {
// TODO: Search for a user specified flower
String flowerType;
Scanner flowerInput = new Scanner(System.in);
System.out.println("Please enter the name of a flower to search for:");
flowerType = flowerInput.nextLine();

for (int i = 0; i < flowerPack.length; i++) { //done in O(n) time This is a linear search
if (flowerPack[i].equals(flowerType)) {
System.out.println("Found your flower " + flowerPack[i]);
break;
} else {
System.out.println("Invalid flower type!");
break;
}
}
}

private void displayFlowers(String flowerPack[]) {
// TODO: Display only the unique flowers along with a count of any duplicates
/*
* For example it should say
* Roses - 7
* Daffodils - 3
* Violets - 5
*/

Map<String,Integer> theFlowers = new HashMap<String, Integer>();

for(int i=0;i<flowerPack.length;i++){

if(theFlowers.get(flowerPack[i])==null){
theFlowers.put(flowerPack[i],1);
}else{
theFlowers.put(flowerPack[i], theFlowers.get(flowerPack[i])+1);
}
}
System.out.println(theFlowers);

}



}

Answer

Do you want to add at max 25 flowers and then stop, or do you want to override the first String when a 26th flower is added?

If you only want to add at max 25 flowers the easiest approach would be to add the new String at the first position with an empty String inside the array. Something like

flowerName = flowerInput.nextLine();
for(int i=0; i<flowerPack.length; i++){
    if(flowerPack[i] == null || flowerPack[i].equalsIgnoreCase("")){
        flowerPack[i] = flowerName;
        break;
    }
}

If you want to rotate on the array (after the 25th flower is added you overwrite the first array element), you have to remember the next Index to add a flowername. The index could be a class variable (but you shoudn't call it 'i' if you want to use a class variable, since 'i' is often used in loops)

Edit: Added flowerPack[i] != null

(The code is untested and most likely still contains errors)

Edit 2: Added a break so the flower name is only saved at one position inside the array.

Comments