0

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?

2
  • 1
    Maybe you should point out where exactly the NullPointerException is happening. My guess is that you can't split on an empty string... Commented Mar 17, 2013 at 4:37
  • I guess split on an empty string would split the 'plaintext' string letter by letter, and so it did. I did an iteration through the array 'splittext', and yes, each element is a single letter. And of course, the exception happens on "numtext[i]=numMap.get(splittext[i]);" Commented Mar 17, 2013 at 4:40

4 Answers 4

4

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 :)

Sign up to request clarification or add additional context in comments.

1 Comment

Exactly! Skipping the first element fixes it. Thank you!
1

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

Comments

1

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);

2 Comments

Yes, the first element, which is an empty string causes the problem.
You sure you didn't just copy from my answer? :)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.