Results 1 to 10 of 10
  1. #1
    sepaht is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default FileReader / Buffered Reader

    Hi there, I have a simple problem you might can help me.. I used FileReader and BufferedReader in order to read data from my pc. My data is a representation of numbers separated by commas. Eg.
    0,1,2,3,4,5
    3,2,2,3,4,5
    6,1,2,3,4,5
    .
    .
    .

    So i did the following function in order to exclude the commas and insert my data into arrays to help me to manipulate the data. :

    Java Code:
    public class LoadData {
       
        DataTool DT = new DataTool();
        String[] Networktemp = null;
        
        int a;
        int b;
        int count=0;
        int dimensionscount=0;
        int classescount=0;
     
        Vector<Integer> classes = new Vector<Integer>();
        Vector<Integer> dimensions = new Vector<Integer>();
        
        public void Loaddata() {      
          try {
               
              File DataFile =DataTool.menu.getSelectedFile();
              FileReader file = new FileReader(DataFile);
              
              BufferedReader buff = new BufferedReader(file);
              boolean eof = false;
                
                dimensionscount=0;
                classescount=0;
                
                while (!eof){
                
                    String lineNetwork = buff.readLine();
                    //System.out.println(" lineNetowork :" +lineNetwork.substring(2));
                    System.out.println("Classes count: " +classescount);
                    
                    if (lineNetwork ==null)
                        eof = true;
                 
                    else {
                        
                     
                        Networktemp = lineNetwork.split(",");
                        classes.addElement(Integer.parseInt(Networktemp[0]));
                        
                        for (int i=1; i<Networktemp.length; i++){
                            dimensionscount++;
                            dimensions.addElement(Integer.parseInt(Networktemp[i]));
                        }
                        
                        classescount++;
    //                    System.out.println(" Classes Vector: " +classes);
    //                    System.out.println(" Dimensions: " +dimensions);
                    }
                }
           
                count = (dimensionscount/classescount);  
                int[][] DataArray = new int[classescount][count+1];
                
                
                for (int i=0; i<classescount; i++){
                    a = classes.elementAt(i);
                    DataArray[i][0] = a;
                }
                for (int q=0; q<count; q++){
                    for (int z=0; z<classescount; z++){   
                        b = dimensions.elementAt(z*5+q);
                        DataArray[z][q+1] = b;
                        System.out.println(" this is dimensions: " +b);
                    }
                }
              
                buff.close();
    //===========================================================================================
    // Print  Array
    //===========================================================================================
                for (int i=0; i<DataArray.length; i++){
                    for (int j=0; j<DataArray[i].length; j++) {
                        System.out.print(" " + DataArray[i][j]);
                    }
                    System.out.println(" ");
                }
          } catch (IOException e){
              System.out.println(" Errror....." +e.toString());
            }
        }

    My problem is .. at the moment i want to use data that in the third line there is a line white space and then it continues normally. Once it starts reading the data and reaches the third line then it crashes because of the condition
    Java Code:
     if (lineNetwork ==null)
    which is used to stop the program until there is no more rows in the txt file.

    So, is there any way that I could "bypass" the first three lines and start from the 4th and start reading the data (i ve tried to use substring on my read line but did not work) any ideas

    Thank you

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Reading a blank line does not return null. It returns an empty String ("") which is something entirely different.

    Where your program crashes is here:
    Java Code:
    Networktemp = lineNetwork.split(",");
    classes.addElement(Integer.parseInt(Networktemp[0]));
    A split on an empty String returns a zero length array and Networktemp[0] results in an IndexOutOfBounds exception.

    Add
    Java Code:
    if (lineNetwork.trim().length() == 0) continue;
    as the first part of your else statement.

  3. #3
    Sarinam is offline Senior Member
    Join Date
    Jun 2008
    Posts
    121
    Rep Power
    0

    Default

    Quote Originally Posted by masijade View Post
    Reading a blank line does not return null. It returns an empty String ("") which is something entirely different.

    Where your program crashes is here:
    Java Code:
    Networktemp = lineNetwork.split(",");
    classes.addElement(Integer.parseInt(Networktemp[0]));
    A split on an empty String returns a zero length array and Networktemp[0] results in an IndexOutOfBounds exception.

    Add
    Java Code:
    if (lineNetwork.trim().length() == 0) continue;
    as the first part of your else statement.
    Yes masijade is right or in simple way you just skip the blank line.

  4. #4
    sepaht is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    I ve tried the condition.. unfortunately it doesnt work ... is there any other way to skip the blank line ?

  5. #5
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Let me see the modified code, and what exactly does "it doesn't work" mean?

    Letrs back up a step, and you give us the stacktrace from the exception that resulted in your original code, and then the stacktrace that results from the problem in the new code.

  6. #6
    sepaht is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    Sorry for my limited experience in Java programming..

    Here is the exception :

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

    it goes through all the rows but it crashes.

  7. #7
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    On which line? That execption print out a file and line number. Somewhere on that referenced line you are using a variable that you haven't defined yet. Which line (post the line, not just the line number because your posted code has no line numbers).

  8. #8
    sepaht is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    ok is on line 53 where i typed the condition.

    Java Code:
    1. public class LoadData {
    2   
    3    DataTool DT = new DataTool();
     4   String[] Networktemp = null;
     5   
     6   int a;
     7   int b;
     8   int count=0;
     9  int dimensionscount=0;
     .   int classescount=0;
     .
     .   Vector<Integer> classes = new Vector<Integer>();
        Vector<Integer> dimensions = new Vector<Integer>();
        
        public void Loaddata() {      
          try {
               
              File DataFile =DataTool.menu.getSelectedFile();
              FileReader file = new FileReader(DataFile);
              
              BufferedReader buff = new BufferedReader(file);
              boolean eof = false;
                
                dimensionscount=0;
                classescount=0;
                
                while (!eof){
                
                    String lineNetwork = buff.readLine();
                    //System.out.println(" lineNetowork :" +lineNetwork.substring(2));
                    System.out.println("Classes count: " +classescount);
                    
                    if (lineNetwork ==null)
                        eof = true;
                 
    [B] 53            if (lineNetwork.trim().length() == 0) continue;[/B]
                    
                    else  {
                        
                    
                        Networktemp = lineNetwork.split(",");
                        classes.addElement(Integer.parseInt(Networktemp[0]));
                        
                        for (int i=1; i<Networktemp.length; i++){
                            dimensionscount++;
                            dimensions.addElement(Integer.parseInt(Networktemp[i]));
                        }
                        
                        classescount++;
    //                    System.out.println(" Classes Vector: " +classes);
    //                    System.out.println(" Dimensions: " +dimensions);
                    }
                }
           
                count = (dimensionscount/classescount);  
                int[][] DataArray = new int[classescount][count+1];
                
                
                for (int i=0; i<classescount; i++){
                    a = classes.elementAt(i);
                    DataArray[i][0] = a;
                }
                for (int q=0; q<count; q++){
                    for (int z=0; z<classescount; z++){   
                        b = dimensions.elementAt(z*5+q);
                        DataArray[z][q+1] = b;
                        System.out.println(" this is dimensions: " +b);
                    }
                }
              
                buff.close();
    //===========================================================================================
    // Print  Array
    //===========================================================================================
                for (int i=0; i<DataArray.length; i++){
                    for (int j=0; j<DataArray[i].length; j++) {
                        System.out.print(" " + DataArray[i][j]);
                    }
                    System.out.println(" ");
                }
          } catch (IOException e){
              System.out.println(" Errror....." +e.toString());
            }
        }
    }

  9. #9
    sepaht is offline Member
    Join Date
    Jul 2008
    Posts
    5
    Rep Power
    0

    Default

    oh I think i found the mistake sorry for my limited java skills thnx for your help very helpful!

  10. #10
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Yes, I believe you did now. The trim should be the first line in the else block.

    edit: Or as an "else if" before the else block. I.E. either shift it down two lines or add an "else" in front of it.
    Last edited by masijade; 07-10-2008 at 09:07 PM.

Similar Threads

  1. Replies: 5
    Last Post: 02-02-2010, 10:06 AM
  2. A simple DOM reader
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-03-2008, 10:24 AM
  3. help with file reader
    By jason27131 in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 04:03 AM
  4. Help with filereader in java
    By zoe in forum Advanced Java
    Replies: 2
    Last Post: 07-26-2007, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •