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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:01:22+00:00 2026-06-10T08:01:22+00:00

I have read a file into an array but am wondering how to parse

  • 0

I have read a file into an array but am wondering how to parse certain values from that array.

My code:

        ...
        try{
             ...
             String strLine;
             String delims= "[ ]+";
             //parsing it
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             System.out.println("Length:" + parsedit.size());
             in.close();   
             ...

The files that I am reading in are like this:

a b c d e f g
1 2 3 4 5 6 7
1 4 5 6 3 5 7
1 4 6 7 3 2 5

Which makes the output like this:

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...
Length:4

I would like to parse out this data and just have the first and fifth values remaining, so that it would read like this instead:

a e
1 5

Does anyone have a recommendation on how to go about it?
EDIT:
Following some of the suggestions I have changed my code to:

public static void main(String[] args) {
        System.out.print("How many files will be input? ");
        Scanner readIn=new Scanner(System.in);
        int input=readIn.nextInt();
        int i=1;
        while(i<=input){
            System.out.println("Hey, please write the full path of the input file number" + i + "! ");
            Scanner fIn=new Scanner(System.in);
            String fileinput=fIn.nextLine();
          try{
             FileInputStream fstream=new FileInputStream(fileinput);
             DataInputStream in=new DataInputStream(fstream);
             BufferedReader br=new BufferedReader(new InputStreamReader(in));
             String strLine;
             String delims= "[ ]+";
             ArrayList parsedit = new ArrayList();
             while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
             }
             String[] splits=strLine.split(delims);
             System.out.println("Length:" + splits.length);
             in.close();   
        }
        catch(Exception e){
            System.err.println("Error: " + e.getMessage());
        }
        i++;
    }
  }
}

This doesn’t give back any errors but it doesn’t seem to be working all the same. Am I missing something silly? The output is this:

How many files will be input? 1
Hey, please write the full path of the input file number1! 
/home/User/Da/HA/file.doc
Being read:
a b c d e f g 
Being read:
1 2 3 4 5 6 7 
...

But despite the fact that I have a line to tell me the array length, it never gets printed, which tells me I just ended up breaking more than I fixed. Do you know what it is that I may be missing/forgetting?

  • 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-10T08:01:24+00:00Added an answer on June 10, 2026 at 8:01 am

    The split() function returns an array of strings representing the tokens obtained from the original String.

    So, what you want is to keep only the first and the 5th token (splits[0] & splits[4]):

      String[] splits = strLine.split(delims); 
      //use the splits[0] & splits[4] to create the Strings you want
    

    Regarding your update, replace this:

     while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 System.out.println(strLine);
                 parsedit.add(strLine.split(delims));
     }
    

    With this:

     while((strLine=br.readLine())!=null){
                 System.out.println("Being read:");
                 String splits[] = strLine.split(delims);
                 System.out.println(splits[0]+" "+splits[4]);
                 parsedit.add(splits);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi there I have to read data from text file into the array in
I have read a file into a String. The file contains various names, one
I'm trying to read a file that I have a web address for into
I'm doing some Wave file handling and have them read from disk into an
I have the following simple code, that reads contents of a text file into
I need to read test.TXT file(tab delimited) into MATLAB. TXT file have form: Datum
I have read that CURL is way too fast than File Get Contents and
When I using the following code to read file: lines=file(data.txt).read().split(\n) I have the following
By using filestreaming in c++, I have read a string in the binary file
Most of the time when we read the file stream into a byte array,

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.