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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:35:04+00:00 2026-06-04T22:35:04+00:00

So I have a file that looks like this: 1st 2nd­ nth e1­, ­­v1,

  • 0

So I have a file that looks like this:

    1st 2nd­ nth
    e1­, ­­v1, 1
    e1, v3, 2
    e1, v4, 4
    e1, v5, 7
    e2, v1, 1
    ., ­., .
    ., ­., .
    ., ­., .

where I want the first column to be the key of a hashmap (e1, or e2, or e3), and the value to be an ArrayList called “Ratings” that in I want the second column to have it’s value (an int), inside the nth index of the arraylist.

Here is my code in it’s entirety so far:

import java.util.*;
import java.io.*;

public class Setup
{
    public static void Setup(String[] args)
    {
        String user;
        int value, location;
        //Create a Hashmap that holds a string key and an ArrayList value
        HashMap<String, ArrayList<Integer>> userRatings = new HashMap<String, ArrayList<Integer>>();
        try
        {
            BufferedReader bufferReader = new BufferedReader(new FileReader("Student list.txt")); //read from file
            String line, sentence; //declare two string variables
            String[] sData; //declare a string array (store the contents here)
            line = bufferReader.readLine(); //Read the line
            while (line != null) //While there is a line, do this:
            {
                line = bufferReader.readLine();
                sData  = line.split(", "); //Into the string array, enter individual values in the line split by the ", " characters
                int iData[] = new int[sData.length]; //Create an int array the size of the string array
                user = sData[0];
                for (int i = 0; i <sData.length; i++) //fill the int array with the int-version of the string array
                {
                    iData[i] = Integer.parseInt(sData[i]); //pass the strings as integers into the integer array
                }
                value = iData[1];
                location = iData[2];
                if(!userRatings.containsKey(user)) //The user does not have ratings.
                {
                    ArrayList<Integer> ratings = new ArrayList<Integer>();
                    //                     ratings = userRatings.get(user);
                    for (int j = 0; j < 50; j++)
                    {
                        ratings.add(j);
                    }
                    System.out.println(user + " " + userRatings.get(user));

                }
                else //The user has ratings
                {
                    userRatings.get(user).add(location,value);
                    System.out.println(user + " " + userRatings.get(user));
                }
            }
            bufferReader.close();
        }    catch (FileNotFoundException e)
        {
            System.out.println("File does not exist or could not be found.");
        }
        catch (IOException e)
        {
            System.out.println("Can't read from file");
        }
        catch (NullPointerException e)
        {
        }
    }
}

I am having a problem with modifying the contents of the arraylist.

To sum up:
Every string in the 1st column of the file would have it’s own key in the hashmap (userList)
The program will check if it has the key, if no key exists, it will create a new arraylist as the value for the key.
The arrayList will be populated with 50 indexes, of which they will contain “0”.
After that, the arraylist will have new values added from the file in which the integer in the second column will be added at the corresponding value of the nth column.

How would I populate the arraylist, and how would I edit it so that if I want to add a new integer at the nth index of user e6, I could?

  • 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-04T22:35:05+00:00Added an answer on June 4, 2026 at 10:35 pm

    When given key is not present in your Map you have to add a new Key value pair

    if(!userRatings.containsKey(user)) //The user does not have ratings.
    {
       ArrayList<Integer> ratings = new ArrayList<Integer>();                   
       for (int j = 0; j < 50; j++) {
           ratings.add(j);
      }
      userRatings.put(user, ratings); // place new mapping 
      System.out.println(user + " " + userRatings.get(user));
    } else //The user has ratings
    {
        ArrayList<Integer> ratings  = userRatings.get(user);
        ratings.add(location,value); // Update your list with location and value
        System.out.println(user + " " + userRatings.get(user)); 
    }
    

    iData[i] = Integer.parseInt(sData[i]); This will not work given your file content it will throw NumberFormatException as v1 cannot be parsed to int.

    Instead you can do something like this:

    value = Integer.parseInt(sData[1].trim().substring(1));
    location = Integer.parseInt(sData[2].trim());
    

    To Compare values of two keys:

    Approach 1

    ArrayList<Integer> first = userRatings.get(e1);
    ArrayList<Integer> second = userRatings.get(e2);
    
    //Taking the smallest size will ensure that we don't get IndexOutOfBoundsException.
    
    int length = first.size() < second.size() ? first.size() : second.size();
    
    for(int iDx = 0; iDx < legth; iDx++){
       //compare content
    }
    

    Approach 2

    ArrayList<Integer> first = userRatings.get(e1);
    ArrayList<Integer> second = userRatings.get(e2);
    
    Arrays.equals(first.toArray(), second.toArray());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file that looks like this: 1st ­ ­ ­ ­­­ 2nd
I have a .plist file that looks like this: <plist version=1.0> <array> <dict> <key>name</key>
I have an XML file that looks like this... <foo> <bar>$VALUE$</bar> </foo> Is there
I have a file that looks like this: @Afghanistan.png, @Albania.png, @Algeria.png, @American_Samoa.png, I want
I have an xml file that looks like this: <args> <sometag value=abc /> <anothertag
I have a file that contain lines that looks like this: >AF001546_1 [88 -
I have a file that contain list of numbers that looks like this: 10^-92
I have a text file that looks like this: value1 value2 value3 There are
I have a php file that looks like this: <?php include(config.php); // put the
I have an XML file that looks like this: <SiteMenuItems> <SiteMenuItem text=Home navigateurl=/Default.aspx tooltip=Return

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.