0

Here is a sample input text file:

some text
one more line of text
another line
and one more
1 23 3
2 34 4
5 12 4
3 23 8

I need to extract each column of numerical values and perform some mathematical operation on it.

This is what i have tried till now:

Path inputFile = Paths.get(args[0]);

            try{
                ArrayList<String> lines = (ArrayList<String>) Files.readAllLines(inputFile, Charset.defaultCharset());
                ArrayList<String []> columns = new ArrayList<>();
                for(String line : lines){
                    columns.add(line.split("\\s"));
                }
                // Now for each line you have columns.
                for(String [] s : columns){ 
                    System.out.println(Arrays.toString(s));
                }

But its just not right and i'm unable to move from this deadlock. Any help would be greatly appreciated..

3
  • use Integer.parseInt(columns.get(4/*line 4*/)[2/*23*/]) to convert one of those strings to an integer. Commented Sep 16, 2013 at 0:45
  • When I run the code, the output is similar to the Input! I want the values of each column to be copied to an array (i.e 3 arrays for 3 columns) so that I can add/do some calculation the values in the array at later stage. Commented Sep 16, 2013 at 0:50
  • So basically you want to transpose the numbers part of the input? You want the data be stored column wise rather than line wise. Commented Sep 16, 2013 at 12:22

1 Answer 1

1

I would say ...

for(String line : lines){
                // Just add this condition
                if(line.matches("^[0-9]"){
                    // then process the way you want to...
                }   
            }

Hope this helps...

Edited:

Path inputFile = Paths.get(args[0]);

    try{
        ArrayList<String> lines = (ArrayList<String>) Files.readAllLines(inputFile, Charset.defaultCharset());
        ArrayList<String []> columns = new ArrayList<>();
        for(String line : lines){
            if(line.matches("^[0-9]")){
                System.out.println("Line:"+line);
                String[] colms = line.split("\\s+");
                    columns.add(colms);
                for (int i = 0; i < colms.length; i++) {
                    String temp = colms[i];
                    System.out.println("Colmns:"+i+":"+temp);
                    // process temp one by one
                }
            }
        }
    }catch(Exception ex){
        ex.printStackTrace();
        System.out.println("Exception...");
    }
Sign up to request clarification or add additional context in comments.

2 Comments

Now my output is blank!
Edited the answer...not sure why it's blank...could you please debug and let me know...

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.