phil
phil

Reputation: 253

Something goes wrong when using HashMap

I am trying to covert a string into a number array, so I defined a hash map to do the job. Here is my code:

import java.util.*;

public class DES {

static HashMap<String, Integer> numMap = new HashMap<String, Integer>();

private static void init(){
    numMap.put("a", 0);
    numMap.put("b", 1);
    numMap.put("c", 2);
    numMap.put("d", 3);
    numMap.put("e", 4);
    numMap.put("f", 5);
    numMap.put("g", 6);
    numMap.put("h", 7);
    numMap.put("i", 8);
    numMap.put("j", 9);
    numMap.put("k", 10);
    numMap.put("l", 11);
    numMap.put("m", 12);
    numMap.put("n", 13);
    numMap.put("o", 14);
    numMap.put("p", 15);
    numMap.put("q", 16);
    numMap.put("r", 17);
    numMap.put("s", 18);
    numMap.put("t", 29);
    numMap.put("u", 20);
    numMap.put("v", 21);
    numMap.put("w", 22);
    numMap.put("x", 23);
    numMap.put("y", 24);
    numMap.put("z", 25);
    numMap.put(" ", 26);
    numMap.put(".", 27);
    numMap.put(",", 28);
    numMap.put("?", 29);
    numMap.put("(", 30);
    numMap.put(")", 31);

}

public static void main(String[] args) {

    init();

    String plaintext = "how do you like computer science";
    String[] splittext=plaintext.split("");
    int[] numtext=new int[splittext.length];
    for(int i=0;i<splittext.length;i++)
    {
        numtext[i]=numMap.get(splittext[i]);
        System.out.println(numtext[i]);
    }
}


}

I got a 'nullpointerexception' when running, but I guess the hash map is ok, since I tried something like

 numtext[i]=numMap.get("z"); 

And it works fine. So maybe there is some issues with my splittext array?

Upvotes: 0

Views: 106

Answers (4)

Ankur Shanbhag
Ankur Shanbhag

Reputation: 7804

You are trying to split the String using an entry string pattern ("") as a result the String does not get split, and you get NullPointerException as you are trying to access the split String (which does not exist). Try splitting the String using ("\s") which will split the string on spaces.

Hope this helps.

Upvotes: 0

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136122

String[] splittext = plaintext.split(""); creates an array with empty strings

[, h, o, w,  , d, o,  , y, o, u,  , l, i, k, e,  , c, o, m, p, u, t, e, r,  , s, c, i, e, n, c, e] 

and numMap.get(""); returns null. You can add an entry with key="" to the map to fix the problem

    numMap.put("", 32);

Upvotes: 1

Andrew Mao
Andrew Mao

Reputation: 36940

Arrays.toString(splittext):

[, h, o, w,  , d, o,  , y, o, u,  , l, i, k, e,  , c, o, m, p, u, t, e, r,  , s, c, i, e, n, c, e, ]

The first element of splittext is the empty string, which has no mapping in your HashMap and hence NullPointerException on get().

You can fix this by skipping the first element of splittext. String#split() has a way to avoid trailing whitespace, but not leading whitespace, so I don't think you can do it any other way.

A better solution is just to use String#toCharArray() which is better than splitting on an empty string :)

Upvotes: 4

Blake
Blake

Reputation: 591

Since you are just splitting the string on every character, I would consider just storing the chars in the map, use plaintext.toCharArray(), and then loop through the result array, looking up each char in your map

Upvotes: 1

Related Questions