2017-05-06 11 views
0

好きな動物などのお気に入りのファイルを読み込むプログラムを作る必要があります。それで、 "Dog、Cat、Fox"というファイルを1行に1つずつ読み上げます。それらはArraylistに入れられます。ユーザーに追加または削除のプロンプトが表示された後、ユーザーはそれらのランク付けを求められます。私が混乱しているHeres - arraylistの行を並べ替え/ランク付けするには、ランキングを入れるために2番目のArrayListが必要でしょうか?また、ランク付けされた後、猫のように「私は猫にアレルギーがある」などのように、それぞれにコメントを追加するよう求められます。これらのコメントのために3番目の配列リストが必要でしょうか?サンプル出力を聞く -このプログラムには複数のArrayListが必要ですか?

「お気に入り」|ランク|今このラウンドからのコメント(プラスすべての以前のコメント)

HERESに私のコードあなたは親切たいの私はそれがすべての作業をdoesntのgoing-だが、あなたはそれ

Scanner input = new Scanner(System.in); 
     ArrayList <String> favoriteAnimals = new ArrayList <String>(); 
     boolean repeat = true; 
     while (repeat) { 

      System.out.println("Enter the name of the file which contains your favorite animals "); 
      String fileName = input.nextLine().trim(); 

      try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) { 

       String line; 
       System.out.println("Here are your favorite animals according to the file:"); 
       while ((line = reader.readLine()) != null) {  
        System.out.println(line); 
        favoriteAnimals.add((line)); 
       }             

       System.out.println("Add more? (y/n)"); 
       if (input.next().startsWith("y")) { 
        System.out.println("Enter : "); 
        favoriteAnimals.add(input.next()); 
       } else { 
        break; 
       } 
       for (int i = 0; i < favoriteAnimals.size(); i++) { 
        System.out.println(favoriteAnimals); 
       } 

        System.out.println("Remove an animal?"); 

        if (input.next().startsWith("y")) { 
         System.out.println("Which animal would you like to remove"); 
          String removeAnimal = input.nextLine(); 
          int index = favoriteAnimal.indexOf(removeAnimal); 
          favoriteAnimals.remove(index); 
          System.out.println(favoriteAnimals); 
        } 
        else { 
         break; 
        } 

        ArrayList <String> ranking = new ArrayList <String>(); 
        int size = favoriteAnimals.size(); 
        String first = favoriteAnimals.get(0); 
        System.out.println("What is your ranking of " + first + " out of " + size +"?");     


      } 



      catch (IOException ex) { 
       System.err.println("Your file does not exist!: " + ex.getLocalizedMessage()); 
       repeat = true; 
      } 
     } 
    } 
} 
+0

使用inherita nceは、上の動物が位置としてのランクのような特定の振る舞いを持ち、あなたのすべての猫にコメントしてから、犬のクラスはそれを継承してから配列リストに追加します。 –

答えて

1
の要旨を買ってあげる場所を確認場合

おそらく、という小文字をString name,String commentなどのフィールドに書き込んでからArrayList<Animal>とします。

0

あなたがオブジェクト指向OOに精通している場合、あなたが持っているデータ構造をカプセル化するクラスを使用することをお勧めします、私は簡単な例を書いて、私はそれが問題を解決するためにあなたを助けることを願っています:

Animalクラス

package com.stackoverflow.q1; 

import java.util.ArrayList; 
import java.util.List; 

public class Animal { 

private String name; 
private Integer rank; 
private List<String> comments=new ArrayList<String>(); 

public String getName() { 
    return name; 
} 
public void setName(String name) { 
    this.name = name; 
} 
public Integer getRank() { 
    return rank; 
} 
public void setRank(Integer rank) { 
    this.rank = rank; 
} 
public List<String> getComments() { 
    return comments; 
} 
public void setComments(List<String> comments) { 
    this.comments = comments; 
} 
@Override 
public String toString() { 
    return "Animal [name=" + name + ", rank=" + rank + ", comments=" 
      + comments + "]"; 
} 
} 

メインクラス

package com.stackoverflow.q1; 

import java.util.ArrayList; 
import java.util.List; 

public class Main { 

public static void main(String[] args) { 

    List<Animal> animals= new ArrayList<Animal>(); 

    Animal dog=new Animal(); 
    dog.setName("dog"); 
    dog.setRank(1); 
    dog.getComments().add("Comment 1"); 

    Animal horse=new Animal(); 
    horse.setName("horse"); 
    horse.setRank(2); 
    horse.getComments().add("Comment 1"); 


    Animal cow=new Animal(); 
    cow.setName("cow"); 
    cow.setRank(1); 
    cow.getComments().add("Comment 1"); 

    animals.add(dog); 
    animals.add(horse); 
    animals.add(cow); 


    for(Animal animal:animals){ 
     System.out.println(animal); 
    } 

} 

} 
関連する問題