first non repeated character in a string giving wrong character using java?

I have written code to print first non repeated character from string everything is working fine.But while printing its character its giving null.Example from Sting input="ttaasjji kkk eee" then it should print 's' as first non repeated character.Following is my java code.

public static void main(String[] args) {
    LinkedHashMap hm = new LinkedHashMap();
    //HashMap hm=new HashMap();
    String input = "ttaasjjikkk eee ";
    input = input.trim();
    for (int i = 0; i < input.length(); i++) {
        char c = input.charAt(i);
        Integer val = (Integer) hm.get(c);
        if (c != ' ') {//to exclude space count
            if (val != null) {
                hm.put(c, val + 1);
            } else {
                hm.put(c, 1);
            }
        }
    }
    System.out.println(hm);//each char count      
    Iterator itr = (Iterator) hm.keySet().iterator();
    while (itr.hasNext()) {
        Object temp = hm.get(itr.next());
        String sTemp = temp.toString();
        int value = Integer.parseInt(sTemp);
        if (value == 1) {
            System.out.println("First non repeated character is: " + hm.get(temp) + "," + temp);
            return;
        }
    }
}

please help me,your help will be appreciated.

Jon Skeet
people
quotationmark

You're using the key-set of the map. You want the entry-set so that you can check the value of each entry. That's what's storing the count - without any need for parsing strings as integers.

You should also fix all of your code to use generics, to avoid all the casting:

import java.util.*;

class Test {
   public static void main(String[] args) {
       // Key type is Character, value type is Integer
       Map<Character, Integer> map = new LinkedHashMap<>();
       String input = "ttaasjjikkk eee ";
       input = input.trim();
       for (int i = 0; i < input.length(); i++) {
           char c = input.charAt(i);
           Integer val = map.get(c);
           if (c != ' ') {
               if (val != null) {
                   map.put(c, val + 1);
               } else {
                   map.put(c, 1);
               }
           }
       }

       System.out.println(map);

       // Enhanced for loop to make it easier to iterate
       for (Map.Entry<Character, Integer> entry : map.entrySet()) {
           if (entry.getValue() == 1) {
               System.out.println("First non repeated character is: " 
                   + entry.getKey());
               return;
           }
       }
   }
}

people

See more on this question at Stackoverflow