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;
}