使用用户输入Arraylists对HashMap进行排序

时间:2014-12-17 01:45:48

标签: java sorting arraylist hashmap

嘿伙计们,我很难弄清楚如何对我的HashMap进行排序,我想知道你们是否可以解决这个问题。我已将代码分解到指定区域,因为它是一个非常长的代码。我还标明了我遇到问题的地方。

import java.io.IOException;
import java.util.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class MeleeItOnYou {

  public static void main(String[] args) throws StringIndexOutOfBoundsException, InputMismatchException {
    String ret = "Y", cont = "N";
    Process exitCode;
    int c, o; // c = character and o = option
    boolean Yes, No;
    Yes = (ret.charAt(0) == 'y' || ret.charAt(0) == 'Y');
    No = (cont.charAt(0) == 'n' || cont.charAt(0) == 'N');
    Scanner keyIn = new Scanner(System.in);

    do {
      System.out.println("\nChoose an option");
      System.out.println("1: Character matchups");
      System.out.println("2: Notable Players for Each Character");
      System.out.println("3: Frame Data");
      System.out.println("4: Character Specific Matchup Guide");
      System.out.println("5: Personal Power Ranking System");

      o = keyIn.nextInt();

      if (o == 5) {
        double RD, t, old, x;
        String tournament, player;
        int a, number_of_players, place;
        place = 0;

        try {
          File file = new File("data.txt");
          FileOutputStream is = new FileOutputStream(file);
          OutputStreamWriter osw = new OutputStreamWriter(is);
          Writer w = new BufferedWriter(osw);
          ArrayList<player> players = new ArrayList<player> ();
          ArrayList<placeDisplay> placeVar = new ArrayList<placeDisplay>();
          System.out.println("1:Add a tournament \t2:View Existing");
          a = keyIn.nextInt();
          if (a == 1) {
            System.out.println("\nEnter tournament name");
            tournament = keyIn.next();
            w.write("Tournament Results");
            w.write("\n-----------------");
            w.write("\n" + tournament + ": Tournament Name");
            w.close();
            System.out.println("\nEnter number of players");
            number_of_players = keyIn.nextInt();
            System.out.println("Enter players");
            for (int i = 0; i < number_of_players; i++) {
              String name = keyIn.next();
              player plr = new player();
              plr.setName(name);
              players.add(plr);
            }
            System.out.println("\nEnter places for");
            System.out.println(players);
            for (int i = 0; i < players.size(); i++) {
              System.out.println("\n" + players.get(i));
              int places = keyIn.nextInt();
              placeDisplay placer = new placeDisplay();
              placer.setPlace(places);
              placeVar.add(placer);
            }
            System.out.println("\nThe Places are as follows");
            for (int i = 0; i < placeVar.size(); i++) {
              HashMap<placeDisplay, player> m1 = new HashMap<placeDisplay, player>();
              m1.put(placeVar.get(i), players.get(i));

              // Here is my problem. I've gotten this far but can't figure where to go from here.

              //Map<placeDisplay, player> sortedMap = sortByValue(m1); 
              //System.out.println(sortedMap);
            }
          }
        } catch (InputMismatchException e) {
          System.out.println("Incorrect Input");
          throw new InputMismatchException();
        } catch (IOException e) {
          System.out.println("Problem writing file to data.txt");
        }
      }
      System.out.println("\nReturn to Main Menu? (Y/N)");
      ret = keyIn.next();
    } while (ret.charAt(0) == 'y' || ret.charAt(0) == 'Y');
    if (cont == "N" || cont == "n") {
      System.out.print("\nFAILURE!");
    } else {
      System.out.print("Not an option!");
    }
  }
}

这是我的player类文件

public class player {

    private String name;

    public void setName(String pName)
    {
        name = pName;
    }

    public String getName()
    {
        return name;
    }

    @Override
    public String toString() {
        return name;
    }
}

这是我的placeDisplay类文件

public class placeDisplay implements Comparable<placeDisplay> {

  private int places;

  public void setPlace(int nPlace) {
    places = nPlace;
  }

  public int getPlace() {
    return places;
  }

  @Override
  public String toString() {
    return Integer.toString(places);
  }

  @Override
  public int compareTo(placeDisplay placeDisplay) {
    return places - placeDisplay.getPlace();
  }
}

希望你们能帮助我!谢谢!

0 个答案:

没有答案