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

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




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

HI

i have some problems understanding file reader and biffer reader, meaning how does the thing read the line (or does it read each character separately- i don't even know this) does it read it as a char, a s tring an int...

(since i cannot post my entire problem at oce ( i dunno why, an error comes up), i'm going to post them one after the other... sry bout that)
Sponsor
Sponsor
Sponsor
sponsor
Shivu




PostPosted: Sat Jun 14, 2008 12:12 am   Post subject: Re: File Reader!- Help!!

so i have to answer a question regarding this topic and i don't know how to implement it.

If one could please help me in this regard, that'd be great Smile

here is the question:
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.
Shivu




PostPosted: Sat Jun 14, 2008 12:13 am   Post subject: Re: File Reader!- Help!!

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

Shivu




PostPosted: Sat Jun 14, 2008 12:14 am   Post subject: Re: File Reader!- Help!!

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!


here's the "data.txt" file... the input can be anything.. but here's something to work with!

3
Hello^World
ICS
HelloWorld
Euphoracle




PostPosted: Sat Jun 14, 2008 12:16 am   Post subject: Re: File Reader!- Help!!

Shivu @ Sat Jun 14, 2008 12:12 am wrote:
so i have to answer a question regarding this topic and i don't know how to implement it.

If one could please help me in this regard, that'd be great Smile

here is the question:
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.


Shivu wrote:
code:

...
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
           
            line2 = buffer.read(); <---------------- line2 = Integer.ParseInt(buffer.readLine()); ?
           
            //temp = buffer.readLine();
                       
         
            for (int i =0; i<line2; i++){
...


First glance.
Shivu




PostPosted: Sat Jun 14, 2008 12:19 am   Post subject: Re: File Reader!- Help!!

ok... would it be then:

code:


String line2;     
                        int num;
         String temp; 
          
         try {
         
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
                
            line2 = buffer.readLine();
                num = Integer.parseInt (line2);
            temp = buffer.readLine();
                           
         
            for (int i =0; i<num; i++){



i have to change the string to int then...right.. cuz u can't read it as an int??
Euphoracle




PostPosted: Sat Jun 14, 2008 12:21 am   Post subject: RE:File Reader!- Help!!

On second thought I looked that up in the wrong JDK once again because the sun website is so helpful as to provide multiple JDKs and I am so brilliant not to see which one I am using. The read procedure would be correct. When you do the second readline, however, I think it's either a) assigning nothing to line2 (you're at the end of the line after the first read() operation) or b) moving ahead one line (due to the readLine() operation) and trying to read line 5 on the last iteration, which doesn't exist.
Shivu




PostPosted: Sat Jun 14, 2008 12:32 am   Post subject: RE:File Reader!- Help!!

ok so: i stuck to reading the line as a String and then changing it to an integer because when read it as a character (i think as a character), the ASCII code was stored instead of the number 3...

so here is my code so far... when i run it, the exception highlight the condition in the if statement... dunno y?

code:

 String line3;
int line2;   
         String temp; 
          
         try {
         
            file = new FileReader ("data.txt");
            buffer = new BufferedReader (file);
                
            line3 = buffer.readLine();
                                line2 = Integer.parseInt(line3);
                               
            temp = buffer.readLine();
                     
            for (int i =0; i<line2; i++){
           
               line = buffer.readLine();
               
           
           
               if (line.length() > temp.length()){
               
                  temp = line;
               }
           
            }
                
                System.out.println (temp);

Sponsor
Sponsor
Sponsor
sponsor
Euphoracle




PostPosted: Sat Jun 14, 2008 12:36 am   Post subject: RE:File Reader!- Help!!

Either line is null (therefore it cannot possibly evaluate line.length) or temp is null (also impossible to evaluate temp.length). Make sure you assign values to those and make sure your program is not assigning them null values. Feel free to try using the statement:
Java:
System.out.println((line==null)?"null":"valid"))

To determine if you have any null values, and work backwards from there, replacing line with temp for the second debugging-assistant statement.
Shivu




PostPosted: Sat Jun 14, 2008 12:40 am   Post subject: RE:File Reader!- Help!!

wow! thanks...

um i just noticed that the loop runs 1 time more than it needs to :S... it shoudl only run 2 times right... but it's actually running three times which will assign a null value to line...
Shivu




PostPosted: Sat Jun 14, 2008 12:44 am   Post subject: RE:File Reader!- Help!!

i have another questions actually.. is there a way to read in the first (integer) in the data.txt file as an inteeger?? meaning without declaring two variables and changing the string one to an int...

and what is buffer.read() and what is buffer.readLine() what's the difference between the two.

and which one read int he values as a char?

yea i have a lot of questions..

thanks for your help!
Zeroth




PostPosted: Sat Jun 14, 2008 12:55 am   Post subject: Re: File Reader!- Help!!

read grabs as much data from the file as you specify OR reads one character. readline grabs one line of text from the file, and the end of the line is delineated by \n(newline character) or \r\n(carriage return, rather old school, found mostly in *nix files).

If you use .read(), you get back the actual binary value of that byte, translated into an integer. What that means is if you are reading from a text file, the values may be in ascii, or they may be in utf-8.

Hope that helps.
Euphoracle




PostPosted: Sat Jun 14, 2008 12:57 am   Post subject: RE:File Reader!- Help!!

buffer.read() in the context you are using it will return an int which seems to be (as you describe it) the ascii (I am assuming this encoding) value of the next (or current, depending on how you want to define it) character. buffer.readLine() reads the full line, excluding the line break character(s) (\n on windows, \r\n in unix-based machines (?))

The buffer.read() method will read the ascii value (otherwise known as (int)('3')) whereas buffer.readLine() will report a string ("3"), which can then be parsed using the nifty Integer.ParseInt method.
Shivu




PostPosted: Sat Jun 14, 2008 12:59 am   Post subject: RE:File Reader!- Help!!

ok so, read for example reads one one character of the sentence as an int?? so you'd get the actual value... ascii..

readLine reads the ENTIRE line as a string?? including \n\r

so if you're tracing a program, you'd count \n\r at the end of each line...

wait, is \n\r at the end of EACH line, so when u use read it also includes the \n\r.. right?

thanks!
Shivu




PostPosted: Sat Jun 14, 2008 1:03 am   Post subject: Re: RE:File Reader!- Help!!

Euphoracle @ Sat Jun 14, 2008 1:57 am wrote:
buffer.read() in the context you are using it will return an int which seems to be (as you describe it) the ascii (I am assuming this encoding) value of the next (or current, depending on how you want to define it) character. buffer.readLine() reads the full line, excluding the line break character(s) (\n on windows, \r\n in unix-based machines (?))

The buffer.read() method will read the ascii value (otherwise known as (int)('3')) whereas buffer.readLine() will report a string ("3"), which can then be parsed using the nifty Integer.ParseInt method.


thank you!


so readLine() does NOT include the \n\r??.... or it DOES include the \n\r? because when it reads the line, doesn't it move to the next line below it?

for my case, i need to read it as a string to parse it into an int > right? because if i read it as an int initially, only the ascii value will be stored... and i can't change that ascii to the int value?

thanks!
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 2  [ 19 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: