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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:28:12+00:00 2026-06-12T15:28:12+00:00

until now I have been reading in a file with BufferedReader line by line,

  • 0

until now I have been reading in a file with BufferedReader line by line, however, now I would like to be able to only store the second word on that line. I have my line stored in a hashmap for easy lookup.

     int i=0;

     HashMap<Integer, String> mapHash = new HashMap<Integer, String>();

    try {
        BufferedReader in = new BufferedReader(new FileReader("file"));
        String st;


        while ((st = in.readLine()) != null) {
            st = st.trim();
            //store the lexicon with position in the hashmap
            mapHash.put(i, st);
            i++;

        }
        in.close();
    } catch (IOException e) {
    }

Could anyone help me out to only read the second word on each line?

Thanks!

  • 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-12T15:28:13+00:00Added an answer on June 12, 2026 at 3:28 pm

    For example

    import java.io.BufferedReader;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.StringTokenizer;
    
    //...
    try (BufferedReader in = new BufferedReader(new FileReader("file"));) {
            Map<Integer, String> mapHash = new HashMap<>();
            int i = 0;
            String st;
    
            while ((st = in.readLine()) != null) {
                st = st.trim();
                StringTokenizer tokenizer = new StringTokenizer(st);
                int j = 0;
                while (tokenizer.hasMoreTokens()) {
                    if (j == 1) {
                        mapHash.put(i, tokenizer.nextToken());
                        break;
                    } else {
                        tokenizer.nextToken();
                        j++;
                    }
                }
                //store the lexicon with position in the hashmap
                i++;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm learning scheme and until now have been using guile. I'm really just learning
I have been using the Git bash for doing my commits until now and
I'm developing a new client-server app (.Net) and have up until now been using
I have a js function which has, until now, always been the callback for
I have a list of elements, and I've been using will_paginate up until now,
I'm a ClearCase newbie and up until now have been used to SVN. Therefore,
I work as a freelance web dev, and up until now have been ftping
I'm building a site using CodeIgniter and up until now things have been going
Until now I have been making my own 1024*1024 sprite sheets by placing the
Until now i have been using a list which keeps track of all unique

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.