I've been beating my head against this small program for several hours now, and no matter how I rewrite it, I keep getting an error on run (it compiles fine):
It's a word-counter that I'm trying to make case-insensitive, probably the hard way. With that middle block commented out, as shown, it runs, so the problem is in there somewhere, and I'm sure it's glaringly obvious to anyone who actually has a clue what he or she is doing. I would greatly appreciate any help you can offer.
Ok let me guess why you have 2 maps. The word map is the actual or source and the word2 map is the lowercase version. And let me guess why you get the NullPointerException... either the words or word2 is null for the else if block.
Anyway since you are doing a "word counter" you really need to define what is a "word". Suppose I have the line or sentence: "Today is sunny." This line has 3 words. So if I assume you put the items into the map correctly you should get:
And when you fetch say "is" you get 2.
But I personally think there can be 2 ways of doing this. It depends on what you really want at the end - just a number or like what you have done (matching word count to the actual word). If you just want a number - given you read a line you can simply split it using the blank or space into a String array and return the array size. If keeping what you have done you can again return the map size.
For the other way putting the words into the map is the key. Once you can try looping the keys and see if it is want you want.