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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:48:16+00:00 2026-05-27T18:48:16+00:00

What I want to do is, read a file with multiple words in a

  • 0

What I want to do is, read a file with multiple words in a single line and then add each word in 2d arraylist. This should be such that [[kevin,kevin,kevin][jobs,jobs,jobs]]

the code below works well but it does like this [[kevin,kevin,kevin,jobs,jobs,jobs]]

It should be done by using nested for, but can someone please help?

public void getReference() throws IOException
    {
        String line=null;

            connectRead("computer");
            //this is a method that reads a file in a format kevin kevin kevin kevin
            try
            {
                reference.add(new ArrayList<String>());
                while ((line=bufferedReader.readLine())!=null)
                {
                    st = new StringTokenizer(line); 

                    for ( int i = 0 ; i < st.countTokens() ; i++)
                    {       
                        reference.get(i).add(st.nextToken());
                        reference.get(i).add(st.nextToken());
                        reference.get(i).add(st.nextToken());
                        reference.get(i).add(st.nextToken());  
                    }

                }
                System.out.println(reference);

                bufferedReader.close();
            }
            catch ( IOException e )
            {
               System.out.println(e);
            }      

    }

text in file looks something like this

kevin usa hacker
wozniak usa hacker
jobs usa hacker

  • 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-27T18:48:17+00:00Added an answer on May 27, 2026 at 6:48 pm

    You are always getting references.get(i), where i=0, so whenever new line is read insertion of token starts from the ArrayList at zeroth index.

    try this, but this structure looks a bit confusing to me. may be showing structure of your input file helps to make code better.

    public void getReference() throws IOException
    {
        String line=null;
    
            connectRead("computer");
            //this is a method that reads a file in a format kevin kevin kevin kevin
            try
            {
                reference.add(new ArrayList<String>());
                int indexOfReferences =0 ;
                while ((line=bufferedReader.readLine())!=null)
                {
                    st = new StringTokenizer(line); 
    
                    for ( int i = 0 ; i < st.countTokens() ; i++)
                    {       
                        reference.get(indexOfReferences).add(st.nextToken());
                    }
                   indexOfReferences++;
    
                }
                System.out.println(reference);
    
                bufferedReader.close();
            }
            catch ( IOException e )
            {
               System.out.println(e);
            }      
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read each line from a text file and store them in
I want to read an xml file, apply a transform, then write to another
I want to read line n1->n2 from file foo.c into the current buffer. I
I need to read a single file using multiple threads under Linux. There are
I want to write multiple structures to a single file. I used fwrite function
I have a text file. I want read that file. But In that if
I want to read a file from a java web application. I don't want
I want to read the file path from html input type=file (the entry selected
I have semicolon-separated .dat file and I want to read that file and store
In PHP, I want to read a file into a variable and process the

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.