2016-03-23 37 views
1

私はユーザが2次元配列に値を追加してから配列を検索して値を表示できるようにするプログラムを開発中です。情報は適切に保管されていますが、表示することができるのは動物の名前であり、食品ではありません。私がグリルする前に、正しい出力を得るためにさまざまな方法を探して実装しました。誰かが私に理解を助けることができれば、私のエラーはかなりシンプルだと確信しています、ありがとう!Javaの2次元配列から値を出力する

/*This program will allow a user to enter information into the zoo 
or search by animal for the type of food it eats*/ 

import java.util.Scanner; 

class zoo { 
    //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(); 
} 
     //prompt the user to search or quit 
     System.out.println("Please enter the name of the animal to search for or Q to quit: "); 
      String animalName = in.nextLine(); 
     animalName = animalName.toUpperCase(); 
     if(animalName.equals("Q")){ 
     System.out.println("Thanks for using the program!"); 
     } 
     else { 
      searchArray(animalName); 
     } 

     return animalFood; 

} 

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++) { 
     for (int j = 0; j < animalFood.length; j++) { 
     arrayItem = animalFood[i][j]; 
     arrayItem = arrayItem.toUpperCase(); 
     if(arrayItem.equals(itemToMatch)){ 
      matchResult = "The animal " + name + " was found in the zoo! It eats " + animalFood[j];   
     } 
     else { 
      //nothing found 
     }  

     } 
    } 
    System.out.println(matchResult); 
    if (food != null) { 
    System.out.println(food); 
    } 
    return animalFood; 
} 

//constructor 
public zoo() { 

} 

//overloaded constructor 
public zoo(int x) { 

    int number = x; 
    animalFood = addArray(x); 
} 

//method to get users choice 
public static int menu() { 

    int selection; 
    Scanner input = new Scanner(System.in); 

    System.out.println("Please make a choice in the menu below"); 
    System.out.println("-------------------------\n"); 
    System.out.println("1 - Add animals and the food they eat."); 
    System.out.println("2 - Search for an animal in the zoo."); 
    System.out.println("3 - Exit the program"); 

    selection = input.nextInt(); 
    return selection;  
} 

//main method 
public static void main(String[] args) { 

    //create a new object 
    zoo myZoo = new zoo(); 

    //variables and scanner 
    int userChoice; 
    int numberAnimals; 
    String animalName = ""; 
    Scanner input = new Scanner(System.in); 

    //call the menu 
    userChoice = menu(); 

    //actions based on user choice 
    if (userChoice == 1) { 
     System.out.println("How many animals would you like to enter information for?"); 
     numberAnimals = input.nextInt(); 
     myZoo.addArray(numberAnimals); 
    } 

    if (userChoice == 2) { 
     System.out.println("Please enter the name of the animal to search for: "); 
     animalName = input.nextLine(); 
     myZoo.searchArray(animalName); 
    } 

    if (userChoice == 3) { 
     System.out.println("Thank you for using the program!"); 
    } 
} 
} 

答えて

0

あなたの問題はsearchArrayにあるようです。あなたはaddArray方法で正しく行ったように、animalFood[i].lengthanimalFood.lengthを交換し

for (int i = 0; i < animalFood.length; i++) { 
    for (int j = 0; j < animalFood.length; j++) { 
    ... 
    } 
} 

:あなたのネストされたforループは、配列の一次元だけの大きさを反復しています。

EDIT

あなたの出力方法が間違っているようにそれはまた見えます。この行で

matchResult = "The animal " + name + " was found in the zoo! It eats " + animalFood[j]; 

animalFood[j]animalFood[i][j]でなければなりません。あなたが見ている奇妙な出力は、配列をStringに変換するJavaの試みです。

第二編集

addArray方法を検討した後、私があなたの配列について間違った仮定を作ったようです。あなたの配列は、各インデックスが2つのアイテム、動物、およびその食品を持つように構成されているようです。

animalFood [0] [0] = '猫の
animalFood [0] [1] = 'キャットフード'
animalFood [1] [0] ='犬
:だから、そうのようになります。 animalFood [1] [1] = 'Dog food'
など

この場合、外側のインデックスを反復するようにループを変更したいと思うでしょう。これは内側のforループをsearchArrayの内部で削除することを意味します。次に、内部配列の最初のインデックスと一致させるアイテムを比較し、一致するものがあれば、2番目のインデックスになります。私は実装をあなたに任せます(これは宿題のように見えます)。私がここで言ったことが間違って聞こえるなら、私に知らせてください。

+0

私はこれまでにこれを試していましたが、まだいくつかの驚くべき出力を受けています: '動物犬が動物園で見つかった!それは食べる[Ljava.lang.String; @ 33909752 ' – javamatrix

+0

@ javamatrix私は答えを更新しました。 –

+0

編集内容を読んだ後、私もそれを試しました。しかし、私は失われている:動物のCATは動物園で見つかった!それは猫を食べる ' – javamatrix