2015-03-08 2 views
0

Je travaille sur une TreeMap avec un itérateur qui lit un fichier texte avec 20 valeurs nom et hexadécimales, par exemple RED; FF0000. D'abord dans le principal j'ai fait un Iterator mais je ne suis pas sûr pourquoi il ne lit pas tous les 20 et l'affichage. Essayez également de les trier en valeur croissante lorsque je les affiche sur la console.Tri et affichage d'une TreeMap en augmentant la valeur Problèmes

Console Affichage:

`00008B=Dark Blue 
013220=Dark Green 
37FDFC=Blue 
7f7f00=Dark Yellow 
8B0000=Dark Red 
B26200=Dark Orange 
D3D3D3=Grey 
FF0000=Red 
FFC0CB=Pink 
FFFFCC=Light Yellow` 

code:

public class coloring extends JFrame implements ActionListener { 

public static TreeMap<String, String> buttonColors; 

// Constructor 
public FP(TreeMap<String, String> buttonColors) throws IOException { 

    super("Hexidecimal Button Map"); 
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    this.setLocationRelativeTo(null); 
    this.buttonColors = buttonColors; 

    setSize(500, 400); 
    setLayout(new FlowLayout()); 
    ButtonGroup buttonGroup = new ButtonGroup(); 

    for (Map.Entry<String, String> coloringButtons : buttonColors 
      .entrySet()) { 

     JRadioButton button = new JRadioButton(coloringButtons.getValue() 
       + " " + coloringButtons.getKey()); 

     button.setActionCommand(coloringButtons.getKey()); 
     button.addActionListener(this); 

     buttonGroup.add(button); 
     add(button); 
    } 
} 

public void actionPerformed(ActionEvent e) { 

    String color = e.getActionCommand(); 
    getContentPane().setBackground(new Color(Integer.parseInt(color, 16))); 

} 

public static void main(String[] args) throws IOException { 

    TreeMap<String, String> buttonColors = new TreeMap<String, String>(); 

    BufferedReader br = new BufferedReader(new FileReader("Input File.txt")); 

    while (true) { 
     String str = br.readLine(); 
     if (str == null) 
      break; 
     if (str.length() == 0) 
      continue; 
     String[] st = str.split("; "); 
     String colorName = st[0].trim(); 
     String colorValue = st[1].trim(); 

     buttonColors.put(colorValue, colorName); 
    } 

    br.close(); 

    Set<Map.Entry<String, String>> buttons = buttonColors.entrySet(); 
    Iterator sortingItr = buttons.iterator(); 

    while (sortingItr.hasNext()) { 

     Map.Entry<String, String> entry = (Map.Entry) sortingItr.next(); 
     System.out.println(sortingItr.next()); 

    } 

    FP obj = new FP(buttonColors); 
    obj.setVisible(true); 
    obj.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 10)); 
} 
} 
+0

ouais son pas de tri en augmentant la valeur hexadécimale lors de l'affichage, l'interface graphique est très bien – FreshCoder

+0

je veux dire affiché dans la console – FreshCoder

Répondre

1

Voir votre code ici:

Set<Map.Entry<String, String>> buttons = buttonColors.entrySet(); 
Iterator sortingItr = buttons.iterator(); 
while (sortingItr.hasNext()) { 
    Map.Entry<String, String> entry = (Map.Entry) sortingItr.next(); 
                ^^^^^^^^^^^^^^^^^ 
    System.out.println(sortingItr.next()); 
         ^^^^^^^^^^^^^^^^^^ 
} 

Voir que vous appelez méthode suivante deux fois. Donc, vous avancez le curseur de l'itérateur en appelant deux fois la méthode suivante sur l'itérateur et vous voyez donc n/2 éléments de carte dans votre sortie. Vous pouvez tout simplement l'entrée d'impression carte plutôt comme:

System.out.println(entry); 
+0

oh désolé que le code 'System.out.println (sortingItr .next()); 'était un test mais il aurait dû être" " – FreshCoder

+0

Alors, est-ce que cela a résolu votre problème? – SMA

+0

ouais il l'a fait et je suis arrivé avec 'System.out.println (entry.getKey() +" "+ entry.getValue());' – FreshCoder