Nested hash map with hash map

212
May 03, 2018, at 05:58 AM
public class HashMapsTest {
    static Scanner sc = new Scanner(System.in);
    public static void main(String[] args) throws FileNotFoundException {
        sc = new Scanner(new FileInputStream("TextFile")); // read a file 
        ArrayList<ArrayList<String>> words = new ArrayList<ArrayList<String>>(); // list to put the words inside 
        StringBuffer sb = new StringBuffer(); // string buffer to read the file 
        String[] protasi = null;
        while (sc.hasNextLine()) {
            sb.append(sc.nextLine() + " ");
        }
        protasi = sb.toString()
                    .split("!| !|! | ! |\\? | \\?| \\? |\\?|\\.|\\. | \\.| \\."); // split the string into sentences
        String[][] leksis = new String[protasi.length][];
        for (int i = 0; i < protasi.length; i++) {
            leksis[i] = protasi[i].split(" , |,| ,| ,| - |-| -|- | -- |--| --|-- | : |:| :|: | ; |;| ;|; | |    | ' |'|"
                                             + " '|' | \" |\"| \" | \"| ` |`| `|` "); // split the sentences into words
        }
        // take the table with the words and put it into a list 
        for (int i = 0; i < protasi.length; i++) {
            String x = "";
            ArrayList<String> line = new ArrayList<String>();
            for (int j = 0; j < leksis[i].length; j++) {
                x = leksis[i][j];
                if (x.length() != 0) {
                    line.add(x);
                }
            }
            if (line.size() != 0) {
                words.add(line);
            }
        }
        HashMap<String, HashMap<String, Integer>> outhm = new HashMap<String, HashMap<String, Integer>>();
        HashMap<String, Integer> inhm = new HashMap<String, Integer>();
        for (int i = 0; i < words.size(); i++) {
            for (int j = 0; j < words.get(i).size(); j++) {
                if (!inhm.containsKey(words.get(i).get(j))) {
                    inhm.put(words.get(i).get(j), 1);
                } else {
                    inhm.put(words.get(i).get(j), inhm.get(words.get(i).get(j)) + 1);
                }
                if (!outhm.containsKey(words.get(i).get(j))) {
                    outhm.put(words.get(i).get(j), inhm);
                } else {
                    outhm.put(words.get(i).get(j), inhm);
                }
            }
        }
        System.out.println(outhm.toString());
    }
}

i want to create a hashmap that return this output:

{’man’:{’i’:3, ’am’:3, ’a’:2, ’sick’:1, ’spiteful’:1, ’an’:1, ’unattractive’:1}, ’liver’: {’i’:1, ’believe’:1, ’my’:1, ’is’:1, ’diseased’:1}, ... }

Instead of that output i get :

{ails={ails=1, about=1, for=1, I=5, sick=1, believe=1, do=1, not=1, and=1, me=1, unattractive=1, man=3, spiteful=1, all=1, a=2, disease=1, liver=1, nothing=1, However=1, is=1, am=3, my=2, an=1, at=1, what=1, diseased=1, know=2, certain=1}, about={ails=1, about=1, for=1, I=5, sick=1, believe=1, do=1, not=1, and=1, me=1, unattractive=1, man=3, spiteful=1, all=1, a=2, disease=1, liver=1, nothing=1, However=1, is=1, am=3, my=2, an=1, at=1, what=1, diseased=1, know=2, certain=1}...}

the input is the following: enter image description here

Rent Charter Buses Company
READ ALSO
How to declare 2D Arrays in Java and Implement them (Procedural Programming)

How to declare 2D Arrays in Java and Implement them (Procedural Programming)

I'm struggling to understand 2D arrays and my exam is in 2 days, I've tried myself but I just can't get my head around it so this is basically my last resort and I really need help understanding them

178
Using the correct JDK or changing default in Netbeans on Linux OS

Using the correct JDK or changing default in Netbeans on Linux OS

Disclaimer: I am new to Java, new to Linux, and new to Netbeans - apologies for any over/under explanation - please ask and I will add/remove info

337
How to extend a class with a member of its own type?

How to extend a class with a member of its own type?

Suppose we need to implement different types of tree with a class called "BaseNode" from which other type of Nodes are derived and it suppose to have an instance variable called parent of its own type, generally it looks like:

200
how to complete this java code about candidates? [on hold]

how to complete this java code about candidates? [on hold]

how to complete the program to get the first candidate if the first has to have the 50% PLUS 1The first round will take the first preference candidate of every valid vote and sum them

176