Computer Science Canada File Reader!- Help!! |
Author: | Shivu [ 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) |
Author: | Shivu [ 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 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. |
Author: | Shivu [ Sat Jun 14, 2008 12:13 am ] | ||
Post subject: | Re: File Reader!- Help!! | ||
my code:
|
Author: | Shivu [ 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 ) 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 |
Author: | Euphoracle [ 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 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:
First glance. |
Author: | Shivu [ Sat Jun 14, 2008 12:19 am ] | ||
Post subject: | Re: File Reader!- Help!! | ||
ok... would it be then:
i have to change the string to int then...right.. cuz u can't read it as an int?? |
Author: | Euphoracle [ 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. |
Author: | Shivu [ 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?
|
Author: | Euphoracle [ 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:
To determine if you have any null values, and work backwards from there, replacing line with temp for the second debugging-assistant statement. |
Author: | Shivu [ 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... |
Author: | Shivu [ 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! |
Author: | Zeroth [ 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. |
Author: | Euphoracle [ 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. |
Author: | Shivu [ 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! |
Author: | Shivu [ 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! |
Author: | Euphoracle [ Sat Jun 14, 2008 1:04 am ] |
Post subject: | RE:File Reader!- Help!! |
It does not include the line break characters. It skips over those and simply returns the value of the line as you would see it in Notepad, for example. The buffer.read() method returns the ascii value (assumed ascii encoded file) of the next character. So if you called it first, it would report 51, the ascii value of "3". |
Author: | Shivu [ Sat Jun 14, 2008 1:06 am ] |
Post subject: | RE:File Reader!- Help!! |
if it does not read the break characters, how does it go to the next line?... but the break characters are always present ? i ask too many questions... sry thanks!! |
Author: | Euphoracle [ Sat Jun 14, 2008 1:24 am ] |
Post subject: | RE:File Reader!- Help!! |
- It skips the break characters, but still situates the position after the break characters. - Without break characters, there's no line break. The exception is the EOF, which is read as a single line from the last break character. |
Author: | Zeroth [ Sat Jun 14, 2008 8:42 am ] | ||
Post subject: | Re: File Reader!- Help!! | ||
It does NOT include the \r\n. (Note the order of the two characters. That is important.) Then it moves onto the next line. Here's an example that might help: say you're building a telnet application. In telnet, all enters are translated into \r\n. Treat the incoming connections like just a file, and use Bufferedreader on them. It will keep trying to read from the connection until it gets the pair \r\n, then it returns all the data its grabbed. Now, to turn an ascii into to a char... have you been taught about casting yet? Here's the solution:
I want you to look at that, and think about what is happening. Its taking the int value... and what? is the char value any different? Or, maybe its the context that is different(for the program)? The idea here is that you will understand more than just solving this one problem, but look at what you've learned, and be able to apply to harder ones. |