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 6823837
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T21:49:51+00:00 2026-05-26T21:49:51+00:00

I’m new to Java, but not new to programming, so as my first project

  • 0

I’m new to Java, but not new to programming, so as my first project I decided to create a .txt-.csv parser for someone at work. I read each line in the .txt file and separate it into separate Maps for sections, subsections, subsubsections, and the subsubsections’ contents. Each Map is then assigned to the Map above it (more on this below). I print everything to it just fine, but when I try to read it I get the following error: "java.lang.String cannot be cast to java.util.Map". The error only appears after the code is run, not while compiling, nor in NetBeans IDE.

My Maps are in the following form with each Object being the Map below it: (Why can’t Java make this easy -_- Associative Arrays are all I want)

(Map)array=<string,Object>
(Map)subarray=<String,Object>
(Map)subsubarray=<String,Object>
(Map)subsubcontents=<String,String>

May not be the most efficient way to read this, plan on converting this to recursive function later, but here is my code, copy-pasted from my project. I put comments at where I’ve found the error to be.

public static Map<String,Object> array=new HashMap<String,Object>();

/* Code for populating the following Maps and pushing them into array
<String,Object>subarray
<String,Object>subsubarray
<String,String>subsubcontents
*/

Set section=array.entrySet();
Iterator sectionI=section.iterator();
while(sectionI.hasNext()) {
    Map.Entry sectionInfo=(Map.Entry)sectionI.next();
    Map<String,Object> subMap=(Map)sectionInfo.getValue();
    Set subSet=subMap.entrySet();
    Iterator subI=subSet.iterator();
    while(subI.hasNext()) {
            Map.Entry subInfo=(Map.Entry)subI.next();
            Map<String,Object> subsubMap=(Map)subInfo.getValue();
            Set subsubSet=subsubMap.entrySet();
            Iterator subsubI=subsubSet.iterator();
            while(subsubI.hasNext()) {
                    System.out.println("test");
                    Map.Entry subsubInfo=(Map.Entry)subsubI.next();
                    Map<String,Object> subcontentsMap=(Map)subsubInfo.getValue();
/*
The above line seems to be causing the issues.
If you comment out the rest of this loop (below this comment)
the error will still appear. If you comment out the rest of this loop
(including the line above this comment) it disappears.
Power of deduction my dear Watson.
*/
                    Set subcontentsSet=subcontentsMap.entrySet();
                    Iterator keys=subcontentsSet.iterator();
                    while(keys.hasNext()) {
                        Map.Entry keyMap=(Map.Entry)keys.next();
                    }
                    Iterator values=subcontentsSet.iterator();
                    while(values.hasNext()) {
                        Map.Entry valueMap=(Map.Entry)values.next();
                    }
            }
    }
}

Any help would be much appreciated. I’ve been struggling with this for a couple of days now.

  • 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-05-26T21:49:52+00:00Added an answer on May 26, 2026 at 9:49 pm

    I think you need to clean up your generics to start with:

    Set<Map.Entry<String, Object>> section = array.entrySet();
    Iterator<Map.Entry<String, Object>> sectionI = section.iterator();
    while (sectionI.hasNext()) {
        Map.Entry<String, Object> sectionInfo = sectionI.next();
        Map<String, Object> subMap = (Map<String, Object>) sectionInfo.getValue(); // is this actually a Map<String, Object>?
        Set<Map.Entry<String, Object>> subSet = subMap.entrySet();
        Iterator<Map.Entry<String, Object>> subI = subSet.iterator();
        while (subI.hasNext()) {
            Map.Entry<String, Object> subInfo = subI.next();
            Map<String, Object> subsubMap = (Map<String, Object>) subInfo.getValue(); // is this actually a Map<String, Object>?
            Set<Map.Entry<String, Object>> subsubSet = subsubMap.entrySet();
            Iterator<Map.Entry<String, Object>> subsubI = subsubSet.iterator();
            while (subsubI.hasNext()) {
                System.out.println("test");
                Map.Entry<String, Object> subsubInfo = subsubI.next();
                Map<String, Object> subcontentsMap = (Map<String, Object>) subsubInfo.getValue(); // somehow a String got in here?
    /*
    The above line seems to be causing the issues.
    If you comment out the rest of this loop (below this comment)
    the error will still appear. If you comment out the rest of this loop
    (including the line above this comment) it disappears.
    Power of deduction my dear Watson.
    */
                Set<Map.Entry<String, Object>> subcontentsSet = subcontentsMap.entrySet();
                Iterator<Map.Entry<String, Object>> keys = subcontentsSet.iterator();
                while (keys.hasNext()) {
                    Map.Entry<String, Object> keyMap = keys.next();
                }
                Iterator<Map.Entry<String, Object>> values = subcontentsSet.iterator();
                while (values.hasNext()) {
                    Map.Entry<String, Object> valueMap = values.next();
                }
            }
        }
    }
    

    Then, you should be more explicit with your declaration of array:

    public static Map<String, Map<String, Map<String, Map<String, String>>>> array = new HashMap<String, Map<String, Map<String, Map<String, String>>>>();
    

    This would ensure that you are putting the correct objects into each of the maps. You will never be able to put a String value where a Map<> is expected because it will not compile. This will allow you to write the following code (without needing casts):

    final Set<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> section = array.entrySet();
    final Iterator<Map.Entry<String, Map<String, Map<String, Map<String, String>>>>> sectionI = section.iterator();
    while (sectionI.hasNext()) {
        final Entry<String, Map<String, Map<String, Map<String, String>>>> sectionInfo = sectionI.next();
        final Map<String, Map<String, Map<String, String>>> subMap = sectionInfo.getValue();
        final Set<Map.Entry<String, Map<String, Map<String, String>>>> subSet = subMap.entrySet();
        final Iterator<Map.Entry<String, Map<String, Map<String, String>>>> subI = subSet.iterator();
        while (subI.hasNext()) {
            final Map.Entry<String, Map<String, Map<String, String>>> subInfo = subI.next();
            final Map<String, Map<String, String>> subsubMap = subInfo.getValue();
            final Set<Map.Entry<String, Map<String, String>>> subsubSet = subsubMap.entrySet();
            final Iterator<Map.Entry<String, Map<String, String>>> subsubI = subsubSet.iterator();
            while (subsubI.hasNext()) {
                System.out.println("test");
                final Map.Entry<String, Map<String, String>> subsubInfo = subsubI.next();
                final Map<String, String> subcontentsMap = subsubInfo.getValue();
                final Set<Map.Entry<String, String>> subcontentsSet = subcontentsMap.entrySet();
                final Iterator<Map.Entry<String, String>> entries = subcontentsSet.iterator();
                while (entries.hasNext()) {
                    final Map.Entry<String, String> entry = entries.next();
                }
            }
        }
    }
    

    All that being said, all of those nested generics look ugly. I’d recommend you create some objects to represent your data.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I want use html5's new tag to play a wav file (currently only supported
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I need to clean up various Word 'smart' characters in user input, including but

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.