Solved

read file to list and store in array

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

816 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

10 Experts available now in Live!

Get 1:1 Help Now