Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript How to declare a specific TreeMap value to a String

marew603

New Coder
I'm trying to run through a previously made TreeMap and convert it into a new one based on what the user inputs. I'm creating a hangman gave and the input is the user's guess and I want to make it so the list narrows based on what the user inputs. For example, if they guess "A", all words with the letter "A" in them will go to group 1. While words without the letter "A" will go in group 2. The group with the largest sum of entries will be returned.

The only thing that doesn't seem to work is when I try to get an entry from the dictionary and declare it as a String so I can compare chars in the string to the input.

JavaScript:
public static TreeMap generateList(TreeMap<String,String> dictionary, char input) {
        TreeMap<String,String> group1 = new TreeMap<String,String>();
        TreeMap<String,String> group2 = new TreeMap<String,String>();
        
        for (int k=0; dictionary.firstEntry()!=dictionary.lastEntry(); k++) {
            String tem = (String) dictionary.get(dictionary.lastKey()); ////// doesn't work
            dictionary.remove(dictionary.lastKey());
            //System.out.println(dictionary);
           // System.out.println(tem); these two are for testing purposes
            for (int i = 0; i<tem.length(); i++) {
                if (tem.charAt(i) == input) {
                    //if (group1.containsKey(tem)) {
                        group1.put(tem, tem);
                    //}
                } else if (i == tem.length()){ //&& !group1.containsKey(tem)){
                    group2.put(tem, tem);
                }
            }
        }
        
        if (group1.size() > group2.size()) {
            return group1;
        } else {
            return group2;
        }
    }
 
System.out.println() is from Java not JavaScript. I think you posted in the wrong section. Ask the Moderator to move your post.

And every game of hanged man I ever played dealt with one word and not a group of words.
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom