7 Replies Latest reply: Jun 28, 2007 8:27 PM by 807605 RSS

    Reading Data from the Text File

    807605
      I'm trying to read the Data that is present in abc.txt.
      The code that I wrote is given below.
      import java.io.*;
      import java.util.*;
      class test
      {
      public static void main(String args[])
      {
      
      
                
                StringBuffer sb=new StringBuffer();
                int rows=0;
                int totalvalues=0;
                int columns=0;
                String s[];
                     
                     try
                     {     
                          //scanner is used to scan the textfile
                          Scanner sc=new Scanner("abc.txt");
                          //scan each line in the text file
                          while(sc.hasNextLine())
                          {
                               sb.append(sc.nextLine());
                               rows++;
                          }
                          
                     }
                     catch(Exception e)
                     {
                          System.out.println("Exception"+e);
                     }     
                             
                s=(sb.toString()).split("     ");
                
                totalvalues=s.length;
                columns=totalvalues/rows;
                
                System.out.println("rows:     "+rows+"     Columns          "+columns)     ;
                                    
                double normaldata[][]=new double[rows+1][columns+1];
                int k=0;
                System.out.println("S length is:     "+s.length);
                try {
                
                for(int i = 1; i < rows; i++)
                {
                     for(int j = 1; j < columns; j++)
                     {
                          normaldata[i][j] = Double.parseDouble(s[k]);
                          k++;
                     }
                }
      
                }
                catch(Exception e)
                {
                     System.out.println("Exception internally:     "+e);
                }
           
                for(int i=1;i<=rows;i++)
                {
                     for(int j=1;j<=columns;j++)
                     {
                          System.out.print(normaldata[i][j]+"     ");
                     }
                     System.out.println();
                
                }
           }
      }
        • 1. Re: Reading Data from the Text File
          807605
          Yes? And?
          • 2. Re: Reading Data from the Text File
            807605
            but I could able to read only 1st element in the abc.txt file.
            • 3. Re: Reading Data from the Text File
              807605
              not even the first element.. when i'm trying to execute the code i was getting the String length=1 and the 1st element as 0.0 .. but 0.0 is not my 1st element in the file
              • 4. Re: Reading Data from the Text File
                807605
                First of all: indent your code sanely. What you have there is difficult to read.

                Why are you concatenating all the lines in the file into one long line, and then splitting it, and then taking the split Strings and turning them back into rows and columns? Wouldn't it be much easier to preserve the existing rows in the file?

                Why are you splitting by " "? Are you certain that the data in the file is in exactly that format?

                What format is the content of the file in, exactly?

                Are you aware that you're creating more rows and columns in the 2d array than you're actually using?

                If the file contains data in a fixed-width format, it would probably be easier to parse it using String.substring().
                • 5. Re: Reading Data from the Text File
                  807605
                  do you need more filepath information? just using "abc.txt" may not be enough. You may need something more like:
                      Scanner sc = new Scanner("C:\\Documents and Settings\\sarath44\\My Documents\\Programming\\java workspace\\forum.java.sun.javaProgramming\\abc.txt");
                  Or whatever it would be on your system. Just a thought.
                  • 6. Re: Reading Data from the Text File
                    807605
                    The Data I'm trying to read is given below
                    1 2 3 4 5 6
                    2 22 33 56 78 7
                    89 76 65 56 45 55
                    88 66 45 67 89 11
                    1 2 3 4 5 6
                    2 22 33 56 78 7
                    89 76 65 56 45 55
                    88 66 45 67 89 11
                    1 2 3 4 5 6
                    2 22 33 56 78 7
                    89 76 65 56 45 55
                    88 66 45 67 89 11
                    1 2 3 4 5 6
                    2 22 33 56 78 7
                    89 76 65 56 45 55
                    88 66 45 67 89 11
                    1 2 3 4 5 6
                    2 22 33 56 78 7
                    89 76 65 56 45 55
                    88 66 45 67 89 14
                    I dont exactly know that I'm trying to read more no.of rows that I require.
                    Cud u plz tell me wat changes do i need to make, so that I can read the data more easily.
                    • 7. Re: Reading Data from the Text File
                      807605
                      First, when you read in a line, don't append it to a giant StringBuffer.
                      Split the line you read and store its values immediately. And use a java.util.List rather than an array, so you won't need to know how many rows there are in advance.

                      Also, rather than splitting on " ", split on "\\s+", which splits on any arbitrary amount of whitespace.

                      Convert the data into the type it represents as soon as you split it, and split it as soon as you read a line in.

                      What does input represent? Does each line represent a separate record? Does each field have a different meaning?