Solved

read file to list and store in array

Posted on 2009-04-09
4
422 Views
Last Modified: 2013-11-23
I had a file I need to read it and store it in array...
I am done with it...

but i found a problem and now i want to read the file into a list and then store it into array...

below is the code that i had written with out reading it into list...

a friend of this forum gave me the below link...

http://helpdesk.objects.com.au/java/how-do-i-read-a-text-file-line-by-line-into-a-list

so can any one tell me how to apply the above concept in above link to my code...


class ReadFile
  {     
        int kMaxLines = 60000;          
        double[][] valuePairs = new double[2][kMaxLines];
        public void fileRead()
        {
                try             
                {       
                        int kMaxLines = 60000;  
                        FileInputStream fstream = new FileInputStream("/output.txt");
                        DataInputStream datastream = new DataInputStream(fstream);
                        BufferedReader br = new BufferedReader(new InputStreamReader(datastream));
                        String strLine;
                        int line = 0;
                        while ((strLine = br.readLine()) != null)
                                {
                                String temp_strs[] = strLine.split("\t");
                                for (int j = 0; j < 2; j++)
                                        valuePairs[j][line] = Double.parseDouble(temp_strs[j]);
                                line++;
                        }
                                datastream.close();
                }
                catch (Exception e)
                {
                        e.printStackTrace();
                }
        }
  }

Open in new window

0
Comment
Question by:shragi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 4

Accepted Solution

by:
aman123_123 earned 500 total points
ID: 24104994

        String filename = "abc.txt";
        List<String> lines = new ArrayList<String>();
        int kMaxLines = 60000;      
        double[][] valuePairs = new double[kMaxLines][2];

        BufferedReader in = new BufferedReader(new FileReader(filename));

        String line = null;

        while (null != (line = in.readLine()))

        {
           
            lines.add(line);

        }

        in.close();
       
        int i = 0;
        for(String singleLine : lines)
        {
            valuePairs[i][0] = Double.parseDouble(singleLine.split("\t")[0]);
            valuePairs[i++][1] = Double.parseDouble(singleLine.split("\t")[1]);
        }
       
        for(double[] a: valuePairs)
        {
            System.out.print(a[0] + "\t");
            System.out.println(a[1]);
        }
    }
0
 

Author Comment

by:shragi
ID: 24105082
double[][] valuePairs = new double[kMaxLines][2];

instead of above format can it be done as shown in my format...
i.e.,

double[][] valuePairs = new double[2][kMaxLines];
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 24105300
what r u trying to do with that code?

while ((strLine = br.readLine()) != null)
                                {
                                String temp_strs[] = strLine.split("\t");
for (int j = 0; j < 2; j++)
                                        valuePairs[j][line] = Double.parseDouble(temp_strs[j]);

the above line just overrites the exisiting!
0
 
LVL 4

Expert Comment

by:aman123_123
ID: 24105323
Yes it can be done in that way, but it is easier to understand using the way I have done.

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question