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: Fri Jun 13, 2008 11:54 pm   Post subject: File Reader!

Hi!

i have a lot of problem with filereader and bufferreader! meaning, i don't know if the thing reads the line as a string or as a char or int... or whatever...

and also, here is a question that i have to write a code for involving using filereader and everything...

i know I did it wrong, bt i don't know how to correct it.
whenever i run it an exceptional comes up... nullpointerexception.
if one could help me regarding this situation, that would be great! thank you very mych Smile

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

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");
            }
     
      }
   }



Thanks!
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: