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?

4
  • Instead of BufferedReader.readLine() you could try using BufferedReader.read() to read character by character to find what you're looking for. Commented Aug 23, 2012 at 14:51
  • Please don't use DataInputStream to read text vanillajava.blogspot.co.uk/2012/08/… Its more confusing than useful. Commented Aug 23, 2012 at 14:52
  • @PeterLawrey What would you recommend instead? Commented Aug 23, 2012 at 15:51
  • Remove it, you don't need it. For a better alternative use FileReader as suggested in the article. Commented Aug 23, 2012 at 16:04

3 Answers 3

1

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);
 }
Sign up to request clarification or add additional context in comments.

5 Comments

I changed your splits[5] to splits[4], and apparently destroyed your concurrent edit. Sorry.
This would go after the "parsedit.add(strLine.split(delims));" line?
It depends on your requirements. splits = strLine.splits(delims) returns the array of strings. => splits = {a,b,c,d,e...} , for the next line splits = {1,2,3,4...} and so on
I am horribly sorry, but I am not sure I understand. Do you mind if I update my question so I can get further help?
The question has been updated. If you et a chance to have another look I would be more than grateful. Thanks so much!
0

You can try this

String[] line = s.split("\\s+");

This way all your elements will be stored in a sequence in the array line. This will allow you to access whatever element you want.

2 Comments

Are you suggesting doing this instead of: parsedit.add(strLine.split(delims)); ?
what i am suggesting is to read the entire page into the String s. and then split the characters of string into an array.
0

You just need to get the first and the fifth value out of the String[] returned by strLine.split(delims).

(I am assuming you know what you are doing in your current code. You are assuming that each line will contains the same number of "columns", delimited by at least 1 space character. It is a fair assumption, regardless.)

2 Comments

But how do you recommend going about getting the first and the fifth value?
@User: The split() function returns an array, which you can get the 1st and 5th value @ index 0 and 4.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.