Donate. I desperately need donations to survive due to my health

Get paid by answering surveys Click here

Click here to donate

Remote/Work from Home jobs

Java Jumbles using treemap

I just tried to complete the jumbles for two files but it could only read and write the first two answers.

import java.util.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.StringTokenizer;

public class JumblesMap
{
    public static void main(String[] args) throws Exception
    {
        if ( args.length < 2 )
        {
            wrong( "Need to pass name of input files." );
        }
        ArrayList<String> dictionary = new ArrayList<>();
        ArrayList<String> jumbled = new ArrayList<>();
        TreeMap<String, String> map1 = new TreeMap<>();
        TreeMap<String, String> map2 = new TreeMap<>();

        BufferedReader wordList = new BufferedReader(new FileReader(new File(args[0])));
        while( wordList.ready() )
        {
            dictionary.add(wordList.readLine());
        }
        wordList.close();

        BufferedReader jumbledList = new BufferedReader(new FileReader(new File(args[1])));
        while( jumbledList.ready() )
        {
            jumbled.add(jumbledList.readLine());
        }

        Collections.sort(jumbled);
        Collections.sort(dictionary);

        for ( int i = 0 ; i < dictionary.size() ; ++i ) 
        {
            String Dword = dictionary.get(i);
            String dCanon = toCanon(Dword);
            map1.put(dCanon,Dword);
        }

        for( int i = 0 ; i < jumbled.size() ; ++i )
        {
            String jWord=jumbled.get(i);
            String jCanon = toCanon(jWord);
            map2.put(jWord,jCanon);
        }

        showResult(map1,map2);
    }

    static String toCanon( String st)
    {
        char[] chars = st.toCharArray();
        Arrays.sort(chars);
        return new String(chars);
    }

    static void wrong( String msg)
    {
        System.out.println( "\nFATAL ERROR: " + msg + "\n" );
        System.exit(0);
    }

    static void showResult(TreeMap map1, TreeMap map2)
    {
        // Get a set of the entries
        Set set = map2.entrySet();
        // Get an iterator
        Iterator i = set.iterator();
        // Display elements
        while(i.hasNext()) 
        {
            Map.Entry me = (Map.Entry)i.next();
            System.out.print(me.getKey() + "   ");
            System.out.println(map1.get(me.getValue()));
        }
        System.out.println();
    }

}

This is my code for reading the dictionary.txt and jumbles.txt, it works fine. But for the tinydictionary.txt and tinyjumbles.txt, it can only print two of them. Plz help me fix this.

tinyDictionary.txt
act
cat
tac
dog
god
post
pots
stop
spot
tops
opts
rat
tar
art
trap
tarp
part
flog
golf
frog
gorp
glossy

tinyJumbles.txt
atc
otsp
gdo
atr
arpt
grof
sylogs

The output I got

arpt   trap
atc   tac
atr   tar
gdo   god
grof   frog
otsp   tops
sylogs   glossy

The correct output

arpt part tarp trap
atc act cat tac
atr art rat tar
gdo dog god
grof frog
otsp opts post pots spot stop tops
sylogs glossy

The requirement for the jumble program, but instead of arraylist, i need to use maps for it.(I use treemap)

Comments