rcj
rcj

Reputation: 661

hashmap.get(char c) returning null for only special chars only in linux

I don't understand this. It only happens in linux (runs fine on my machine). It only happens when there are special characters in the file that I'm reading. Could this be a problem with linux and Character.equals()?

Code in another function calling HashMap.get(). This prints "null" for each Hashmap.get() using a special char like Ã.

fis = new FileInputStream(fromFile);
int fromCharInt;
//read a byte at a time from the file
while ((fromCharInt = fis.read()) != -1) {
     System.out.println((char)fromCharInt);  //prints Ã
     System.out.println(hMap.get((char)fromCharInt));  //prints null
}

Generating the HashMap

private static HashMap<Character, String> generateMap(HuffmanTree hTree, List<FreqTracker> freqs)
{
    HashMap<Character, String> hMap = new HashMap<Character, String>();
    BinaryNode<FreqTracker> charNode;
    for (FreqTracker freq: freqs)
    {
        charNode = HuffmanTree.findCharNode(freq.getC(), hTree.getRoot());

        hMap.put(freq.getC(), HuffmanTree.getBinaryCode(charNode, ""));
    }

    return hMap;
}

Upvotes: 0

Views: 225

Answers (1)

rolfl
rolfl

Reputation: 17707

You are reading a single byte from the InputStream (that's what InputStreams do...). Change it to a *Reader and open it with the correct character encoding, and read chars from the Reader, not bytes. Special characters are typically multiple bytes, and Linux is typically UTF-8 encoding by default, hence your problems

Upvotes: 1

Related Questions