1

First off I am aware of ArrayLists and do not want to use them for this program. I am working on a program for a local zoo (very beginning stages and will not be the final version). It takes user input and allows them to enter animals and the foods they eat. My program works fine if I enter one food at a time, but what I am struggling with is figuring out how to handle user input where they add more than one food. (Prompts will guide them to enter food separated by commas, however some users may just use spaces in between and I need to account for that either by an error message or simply accepting it and CSV.) Is there a way to do this and later retrieve the values adding commas back in? Sorry I am pretty new to Java, thanks for any help!

Code for the user input:

 //create array
    static String[][] animalFood;

    String[][] addArray(int x) {

     animalFood = new String[x][2];
     Scanner in = new Scanner(System.in);

   //loop through array and add amount of items user chose
    for (int row = 0; row < animalFood.length; row++){
        System.out.print("Enter an animal name: ");
        animalFood[row][0] = in.nextLine();
        System.out.print("Enter the food the animal eats: ");
        animalFood[row][1] = in.nextLine();
        }

        System.out.println("Thank you for adding information to the zoo!");
        System.out.println("You entered the following information: ");

        //loop through and print the informationa added
        for(int i = 0; i < animalFood.length; i++)
        {
           for(int j = 0; j < animalFood[i].length; j++)
           {
            System.out.print(animalFood[i][j]);
            if(j < animalFood[i].length - 1) System.out.print(" - ");
             }
            System.out.println();
}

Code for the search function:

String[][] searchArray(String name) {

   String matchResult = "There was no " + name + " found in the zoo!"; 
   String itemToMatch = name.toUpperCase();
   String arrayItem = "";
   String food = "";
   for (int i = 0; i < animalFood.length; i++) {
         arrayItem = animalFood[i][0];
         arrayItem = arrayItem.toUpperCase();
         if(arrayItem.equals(itemToMatch)){ 
            matchResult = "The animal " + name + " was found in the zoo! It eats " + animalFood[i][1];        
         }
         else {
            //nothing found
         }      
    }
    System.out.println(matchResult);
    if (food != null) {
    System.out.println(food);
    }
    return animalFood;
}
javamatrix
  • 27
  • 5
  • How do you determine that the user input is incorrect and needs to be modified? For example if user inputs "carrot; red wood;", is this need to be separated to "carrot; red; wood;"? – Nier Apr 22 '16 at 00:59
  • Possible duplicate: http://stackoverflow.com/questions/3481828/how-to-split-a-string-in-java – Nier Apr 22 '16 at 01:02

1 Answers1

0

You can use the split() method of String to break it into an array of strings separated by a given delimiter.

for(int row = 0; row < animalFood.length; row++){
    System.out.print("Enter an animal name: ");
    animalFood[row][0] = in.nextLine();
    System.out.print("Enter the foods the animal eats: ");
    //all of the foods separated by commas
    String[] foods = in.nextLine().split(",");
    for(int i = 0; i < foods.length; i++) {
        animalFood[row][i + 1] = foods[i];
    }
}

You would have to increase the max index of the second dimension of animalFood, though, to account for multiple foods (or, although this is more complicated, you could dynamically resize the array by copying it into an array with a greater max index).

For the search, you would have to nest another loop to list all of the items.

for (int i = 0; i < animalFood.length; i++) {
     arrayItem = animalFood[i][0];
     arrayItem = arrayItem.toUpperCase();
     if(arrayItem.equals(itemToMatch)){ 
        matchResult = "The animal " + name + " was found in the zoo! It eats ";
        //Iterates over the foods it eats
        for(int j = 1; j < animalFood[i].length; j++) {
            //If this is the last food in the list
            if(j == animalFood[i].length - 1) {
                matchResult += "and " + animalFood[i][j] + ".";
            }
            else {
                matchResult += animalFood[i][j] + ", ";
            }
        }        
     }
     else {
        //nothing found
     }      
}
Evan Hall
  • 58
  • 1
  • 8