Solved

read file to list and store in array

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

690 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