Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-10-2008, 02:19 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
sepaht is on a distinguished road
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. :

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
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
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 07-10-2008, 02:28 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
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:
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
Code:
if (lineNetwork.trim().length() == 0) continue;
as the first part of your else statement.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-10-2008, 02:35 PM
Senior Member
 
Join Date: Jun 2008
Posts: 121
Rep Power: 0
Sarinam is on a distinguished road
Default
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:
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
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.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-10-2008, 02:45 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
sepaht is on a distinguished road
Default
I ve tried the condition.. unfortunately it doesnt work ... is there any other way to skip the blank line ?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 07-10-2008, 02:49 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 07-10-2008, 03:07 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
sepaht is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 07-10-2008, 03:10 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
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).
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 07-10-2008, 06:38 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
sepaht is on a distinguished road
Default
ok is on line 53 where i typed the condition.

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;
             
 53            if (lineNetwork.trim().length() == 0) continue;
                
                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());
        }
    }
}
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 07-10-2008, 06:53 PM
Member
 
Join Date: Jul 2008
Posts: 5
Rep Power: 0
sepaht is on a distinguished road
Default
oh I think i found the mistake sorry for my limited java skills thnx for your help very helpful!
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 07-10-2008, 09:05 PM
Senior Member
 
Join Date: Jun 2008
Posts: 1,397
Rep Power: 3
masijade is on a distinguished road
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.
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
FileReader Vs FileInputStream and same goes to output unhurt New To Java 5 02-02-2010 10:06 AM
A simple DOM reader Java Tip Java Tips 0 01-03-2008 10:24 AM
Buffered Streams JavaForums Java Blogs 0 12-26-2007 03:01 PM
help with file reader jason27131 New To Java 1 08-01-2007 04:03 AM
Help with filereader in java zoe Advanced Java 2 07-26-2007 10:55 AM


All times are GMT +2. The time now is 01:13 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org