java.lang.ClassCastException when String input can not use for sum of integers in java


In this Example we are going to understand the exceptions when we have a different type values for all keys and values in the HashMap.

Error Message :
Exception in thread "main" java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Integer (java.lang.String and java.lang.Integer are in module java.base of loader 'bootstrap')
       at com.si.hashmap.HashMapOperations.main(HashMapOperations.java:16)

Error Description :

package com.si.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapOperations {

       public static void main(String[] args) {
              Map intMap = new HashMap();
              intMap.put("First", 568);
              intMap.put("Second", 123);
              intMap.put("third", "third");
              int sum = (int) intMap.get("First")+(int) intMap.get("third");
              System.out.println(sum);
              }
       }
      
In the above example ,the red background color line is giving the exception.
The First and Second Keys we are setting the values as 568,123 as integers but the third one we have given values as "third" as a String.
When we are sum up the key values then the sum won't happen with int and String.so this kind of exception will occur.

Solution:

To avoid this kind of cast exceptions Java have Generics concept. This will help at the compile time itself instead of runtime. The program wont compile if we give string int place of integer.


package com.si.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

public class HashMapOperations {

       public static void main(String[] args) {
              Map<String,Integer> intMap = new HashMap<String,Integer>();
              intMap.put("First", 568);
              intMap.put("Second", 123);
              intMap.put("third", "third");///compilation error
              int sum = (int) intMap.get("First")+(int) intMap.get("third");
              System.out.println(sum);

              }
       }
      
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
       The method put(String, Integer) in the type Map<String,Integer> is not applicable for the arguments (String, String)

       at com.si.hashmap.HashMapOperations.main(HashMapOperations.java:15)

So we can add an integer in the place of String .

package com.si.hashmap;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;


public class HashMapOperations {

       public static void main(String[] args) {
              Map<String,Integer> intMap = new HashMap<String,Integer>();
              intMap.put("First", 568);
              intMap.put("Second", 123);
              intMap.put("third", 789);
              int sum = (int) intMap.get("First")+(int) intMap.get("third");
              System.out.println(sum);

              }
       }
      

Output:
1357


Share this post :

Post a comment

Please share your valuable feedback and share this article in social media.

 
Support : Ranga Rajesh Kumar
Copyright © 2019. Error Solution - All Rights Reserved
Site Designed by Ranga Rajesh Kumar