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
Solved

read file to list and store in array

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
pagenation logic how it is working in my code 1 53
Java: The Public Class Main 4 30
jsp error 6 25
Tagging and Merging on Branch 1 29
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

839 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