2017-10-22 17 views
0

私は学校でこれを行う必要があります。 JAVAプロジェクトであるはずです。我々は、入力を与える場合名前と役割を入力し、名前を付けてその役割の出力を取得します

したがって、たとえば、:

thomas teacher 
charlie student 
abe janitor 
jenny teacher 

は、出力は次のようになります。

teachers,thomas,jenny 
students,charlie, 
janitor,abe. 

が、私はちょうど初心者ですので、これまでのところ、私はこのコードを持っています

`Scanner in = new Scanner(System.in); 
    String line = in.nextLine(); 

    String[] words = line.split(" "); 
    //TreeMap treemap = new TreeMap(); 
    ArrayList<String> admin = new ArrayList<String>(); 
    Scanner input = new Scanner(System.in); 
    while(true){ 
     Boolean s = input.nextLine().equals("Done"); 
     //treemap.put(line, "admin"); 
     if(words[1].contentEquals("admin")){ 
      admin.add(words[0]); 
     } 
     else if(s == true){ 
      break; 
     } 
    } 
    System.out.println("admins," + "," + admin);` 

私はもともとはツリーマップを使用していましたが、それを動作させる方法がわからないので、ArrayList最後にブラケットを取り除きます。

EDIT:

だから私は今、コードを持っている:

HashMap<String, String> teacher = new HashMap<String, String>(); 
    HashMap<String, String> student = new HashMap<String, String>(); 
    HashMap<String, String> janitor = new HashMap<String, String>(); 
    System.out.println("Enter a name followed by a role."); 
    Scanner in = new Scanner(System.in); 
    String line = in.nextLine(); 
    Scanner name = new Scanner(System.in); 
    String r = name.nextLine(); 
    while(true){ 
     if(line.equals(r + " " + "teacher")){ 
      teacher.put(r, "teacher"); 
     } 
    } 
+0

あなたは)HashMapのキーは指定と値を保持します。つまり、配列リストには名前があります – arjunsv3691

+0

配列リストは良い選択です氷。各行には基本的に 'name'、 'position'という値のキーペアが用意されています。したがって、3つの配列リストがあり、2番目の値または 'position'で各配列リストに入る名前をフィルタリングすると、3つの配列リストが必要になります。 – luckydog32

答えて

0

あなたはそれを自分で行う必要がありますので、私はあなたにヒントを与えるでしょう。

HashMap<String, List<String>> mapを使用し、以下のようなあなたの入力を挿入します。

if(map.containsKey(words[1])) 
{ 
List<String> list = map.get(words[1]); 
list.add(words[0]); 
map.put(words[1],list); 
} 
else 
{ 
map.put(words[1],Arrays.asList(words[0])) 
} 

あなたは、各タイプ(student/teacher)に対応するnamesのリストを取得します。この方法をなど

その反復処理後mapとプリントオーバーlist

+0

私はちょうど私が今持っている新しいコードを追加しました。 あなたのコードから、指定された特定の役割の中に入力がどのように名前を入れますか? – Steam

+0

@Steam Hashmapsは、キー値のペアを持つ構造体に情報を格納します。キーは基本的に「検索用語」であり、値はあなたが気にしている実際の情報です。上のケースでは、マップ 'HashMap > map'は、それが職業となるタイプStringのキーであり、値は名前を格納する文字列のリストです。これはたくさんの職業を持っている場合には良いことですが、わずか3人で過度の過労です。 – luckydog32

+0

@ luckydog32私は好奇心が強いです。私はハッシュマップで多くの経験を持っていませんが、上記のコードをハッシュマップを使って記述するとどうなりますか?私にそれを見せることができますか? – Steam

0

私は職業の少量のために、これだけの配列リストを使用してこれを達成するのが妥当だと思います。あなたがしたい場合

private List<String> teachers = new ArrayList<>(); 
private List<String> students = new ArrayList<>(); 
private List<String> janitors = new ArrayList<>(); 

public void seperatePeople(){ 
    Scanner in = new Scanner(System.in); 
    while(true){ 
     //Keep getting the next line in an infinite loop 
     String line = in.nextLine(); 
     if(line.equals("Done")){ 
      break; //end the loop 
     }else{ 
      //Split on the spaces 
      String[] personArray = line.split(" "); 
      //Remember each line is structured like : name, occupation 
      //So when we split the line the array list we get from it 
      //will be in the same order 
      putInArray(personArray[0], personArray[1]); 
     } 
    } 

    //Do whatever printing you have to do down here 

} 

private void putInArray(String name, String occupation) { 
    //filter and add to the correct list in here 
} 

:私は、私はその部分を行う方法の一例であなたを助け、あなたが自分でフィルタリングを処理してもらおうので、あなたが問題を抱えている部分が入力構造だと思います入力方式が同じであるが、代わりにあなたは配列リストを作成し、あなたが行くようにハッシュマップ内に置く3既製の占領のArrayListに名前を入れるのでしょうハッシュマップを使ってこれを実装:

private HashMap<String, List<String>> peopleHashMap = new HashMap<>(); 

public void seperatePeople(){ 
    Scanner in = new Scanner(System.in); 
    while(true){ 
     //Keep getting the next line in an infinite loop 
     String line = in.nextLine(); 
     if(line.equals("Done")){ 
      break; //end the loop 
     }else{ 
      //Split on the spaces 
      String[] personArray = line.split(" "); 
      //Remember each line is structured like : name, occupation 
      //So when we split the line the array list we get from it 
      //will be in the same order 
      putInArray(personArray[0], personArray[1]); 
     } 
    } 

    //You can get all the keys that you created like this 
    List<String> keys = new ArrayList<>(peopleHashMap.keySet()); 

} 

private void putInArray(String name, String occupation) { 
    if(peopleHashMap.containsKey(occupation)){ 
     //If the key (occupation in this case) is already in the hashmap, that means that we previously 
     //made a list for that occupation, so we can just the name to that list 

     //We pull out a reference to the list 
     List<String> listOfNames = peopleHashMap.get(occupation); 
     //And then put the new name into that list 
     listOfNames.add(name); 
    }else{ 
     //If the key isn't in the hashmap, then we need to make a new 
     //list for this occupation we haven't seen yet 
     List<String> listOfNames = new ArrayList<>(); 
     //We then put the name into the new list we made 
     listOfNames.add(name); 
     //And then we put that new list with the into the hashmap with the occupation as the key 
     peopleHashMap.put(occupation, listOfNames); 
    } 
} 
+0

ねえ!私はコードを作った!ただし、arrayListには角括弧が付きます。 Javaでこれらの括弧を取り除く簡単な方法はありますか?また、名前をアルファベット順に並べる方法はありますか?名前がbで始まっているように、最初に名前を入力してもcで始まる名前から始めてください。 – Steam

+0

ニース!どのようにしてあなたのリストを実装するのですか?あなたはハッシュマップでそれをしましたか別々のリストで行きましたか? – luckydog32

+0

私は別のリストでそれをやった!そしてputInArrayメソッドでは、別々のif文で各人の職業を追加しました。だからoccupation.equals( "教师")それは教師Arraylistなどの名前で追加されます! しかし、私は各グループの名前をアルファベットで括弧を削除する必要があります。 – Steam

関連する問題