2

Hey guys I'm having trouble figuring out how to sort my HashMap and I wanted to see if you guys could figure it out. I've broke up my code into the designated area as it is a pretty long code. I've also marked where I'm having the issue.

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!");
    }
  }
}

This is my player class file

public class player {

    private String name;

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

    public String getName()
    {
        return name;
    }

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

And this is my placeDisplay class file

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

Hope you guys can help me out! Thanks!

b4hand
  • 9,550
  • 4
  • 44
  • 49
  • There isn't really a concept of "sorting" a `HashMap`, as the organization of the data is dependent on the hash function used to store your keys. You can indeed sort the keys/values in the map as individual collections by using the `keySet()` and `values()` methods accordingly... – Ryan J Dec 17 '14 at 01:50
  • Are you looking for http://stackoverflow.com/questions/109383/how-to-sort-a-mapkey-value-on-the-values-in-java ? – Louis Wasserman Dec 17 '14 at 01:50
  • That looks like what I want. It sorts the values correctly. I'm very new to programming so idk how to implement that class separately but in the same method. I believe I've mapped the two ArrayLists correctly as previous iterations of my code printed them together correctly. I've only had problems sorting the map as opposed to generating the map itself. If that makes sense. @Louis – Kevin Lewis Dec 17 '14 at 02:00
  • However the problem is with that code is that it uses `HashMap` vs. my `HashMap` @Louis – Kevin Lewis Dec 17 '14 at 02:29
  • What exactly do you want to sort by? – Louis Wasserman Dec 17 '14 at 02:31
  • @Louis placing, with the correct player attached to that place. – Kevin Lewis Dec 17 '14 at 02:34
  • Do you just want it in the original order? Then you can just use a LinkedHashMap instead of a HashMap. – Louis Wasserman Dec 17 '14 at 02:35
  • If you need to sort by the natural ordering of the keys (which implement `Comparable`) you can use a `SortedMap` like a `TreeMap` instead. – Giovanni Botta Dec 17 '14 at 03:04

0 Answers0