Solved

read file to list and store in array

Posted on 2009-04-09
4
417 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
  • 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

705 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now