Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8086753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T18:32:58+00:00 2026-06-05T18:32:58+00:00

My Java application is displaying some odd behaviour, and I’m having trouble finding a

  • 0

My Java application is displaying some odd behaviour, and I’m having trouble finding a solution.

This code snippet takes the contents of a LinkedHashMap where the keys and values are both of type String and writes them to a text file. configWriter is of type PrintWriter.

for (Map.Entry<String, String> entry : configMap.entireSet()) {

    configWriter.println(entry.getKey() + "=" + entry.getValue());
}

The map was declared:

LinkedHashMap<String, String> configMap = new LinkedHashMap<String, String>();

It is populated by reading tokens from a text file using the Scanner class.

One of the values in the map is a String that contains only numbers (an integer), but when the loop gets to this value, it throws a ClassCastException saying that you cannot cast from Integer to String. This makes perfect sense, but the type of the values is String.

Is there a way to force Java to keep this value as a String?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T18:32:59+00:00Added an answer on June 5, 2026 at 6:32 pm

    It’s almost certain that rawtypes are getting used somewhere and there’s an Integer sneaking its way into the map.

    The easiest way to identify where an Integer is getting inserted into the map is to replace the initialization of configMap with

    Map<String, String> configMap = Collections.checkedMap(
      new LinkedHashMap<String, String>(), String.class, String.class);
    

    which will actually throw an exception if something other than a String gets inserted into the map. I wouldn’t personally use that in production code, but this is probably the simplest way to “smoke out” the bug; you’ll get an exception and a stack trace for where exactly the Integer is getting inserted.

    As it stands, the way generics are implemented with type erasure means that there’s nothing actually stopping non-String values from being inserted into the map. The compiler makes some effort to prevent it, but rawtypes and legacy code can get around it. Using Collections.checkedMap will force the issue, though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my java swing application am having a Jframe and Jlabel for displaying current
My Java application has got a package structure similar to this: src/com/name/app src/com/name/app/do src/com/name/utils/db
I'm developing a Java application that uses java.util.logging for its logging needs. This application
I'm designing a Java application using SWT. I have some tables which will display
For some reason the script below is not working. This is the code I
I am having issues displaying a wait cursor in my application. Whenever the mouse
When i'm displaying image from url with using below code, application is taking too
My Java application is started from within a native program through java.dll. This native
Whilst investigating an internationalisation issue in some Java code whereby on a Japanese Windows
I am using the JFreeChart API to generate some chart in my Java application.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.