在Java中添加到数组之前删除字符

时间:2016-04-22 00:18:16

标签: java

首先我知道ArrayLists并且不想在这个程序中使用它们。我正在为当地动物园制作一个项目(非常开始阶段,不会是最终版本)。它需要用户输入并允许他们进入动物和他们吃的食物。如果我一次输入一种食物,我的程序运行正常,但我正在努力解决的是如何在添加多种食物的情况下处理用户输入。 (提示将引导他们输入以逗号分隔的食物,但是有些用户可能只是在两者之间使用空格,我需要通过错误消息或只是接受它和CSV来解释它。)有没有办法做到这一点以及稍后检索重新添加逗号的值?对不起,我对Java很陌生,感谢您的帮助!

用户输入代码:

 //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();
}

搜索功能代码:

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

1 个答案:

答案 0 :(得分:0)

您可以使用String的split()方法将其分解为由给定分隔符分隔的字符串数组。

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

你必须增加animalFood第二维的最大索引,以便考虑多种食物(或者,虽然这更复杂,你可以通过将数组复制到一个具有更大最大值的数组来动态调整数组大小索引)。

对于搜索,您必须嵌套另一个循环以列出所有项目。

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