Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 File Reader!
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Shivu




PostPosted: Sat Jun 14, 2008 12:03 am   Post subject: File Reader!

hi!

i have a lot of trouble regarding filereader and bufferreader. naeming i don't know if the thing reads the line or character (i don't even know how it reads the line: as each character or as a line) as an int, a char, or string...

There is a question i have to write code for regarding filereader and buffer reader...

I'm very confused on this topic... oh along with the try and catch thing...that's what we've to write with filereader and bufferreader.. right?

So if one could help me with this, that would be great :Smile

but first, the question, i really need help with this:

In the text file ?data.txt?, the first line contains an integer which indicates the number of lines that follow. You program should read each one of these lines and then output on the screen the one with the longest length.

my code:

code:


   import java.io.*;

    public class Test {
   
       public static void main (String [] args){
     
         FileReader file;       
         BufferedReader buffer;
         String line;
         int line2;     
         String temp; 
          
         try {
         
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
                
            line2 = buffer.read();
                
            temp = buffer.readLine();
                           
         
            for (int i =0; i<line2; i++){
           
               line = buffer.readLine();
               
           
           
               if (line.length() > temp.length()){
               
                  temp = line;
               }
           
            }
                
                System.out.println (temp);
                
         
         }         
             catch ( IOException iox ) {
               System.out.println("Problem reading data.txt");
            }
     
      }
   }



My code is wrong because whenever i try to run it an exception comes up: nullpointerexception...

so, i don't know how to rectify it....

and if you can explain the logic / concept, that'd be great (or explain what you are trying to say Smile)

thank you!
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: