Computer Science Canada file io |
Author: | person [ Sun Oct 08, 2006 11:31 am ] | ||
Post subject: | file io | ||
the code i have below only reads every other line it there any way for me to make it read every line in a file?
|
Author: | Tony [ Sun Oct 08, 2006 12:31 pm ] | ||
Post subject: | |||
you're in.readLine twice, but output just once. |
Author: | CroAnte [ Fri Oct 13, 2006 10:07 am ] | ||||
Post subject: | Re: file io | ||||
person wrote: the code i have below only reads every other line
it there any way for me to make it read every line in a file?
Change your do structure to:
|
Author: | wtd [ Fri Oct 13, 2006 11:19 am ] | ||
Post subject: | Re: file io | ||
CroAnte wrote: Change your do structure to:
Close, but not quite. You see, the "text" variable is scoped locally to the block in this case. Therefore the test, which is outside the block, is unaware of its existence. |
Author: | zylum [ Fri Oct 13, 2006 6:55 pm ] | ||
Post subject: | |||
i think while is more appropriate than do while:
|
Author: | HellblazerX [ Sun Oct 15, 2006 2:15 pm ] | ||||
Post subject: | |||||
I'm not too sure that would work. You're trying to compare a statement (or is it an expression) with a value, which you can't do. If you're going to have while loops, you'll need to initialize the str variable before the loop:
|
Author: | [Gandalf] [ Sun Oct 15, 2006 6:09 pm ] | ||||
Post subject: | |||||
Ah, but what if statements return a value too? Try it.
Or more obviously:
|
Author: | wtd [ Sun Oct 15, 2006 6:17 pm ] |
Post subject: | |
Yes, C-family languages pretty much erase the barrier between statement and expression. |
Author: | CroAnte [ Wed Oct 18, 2006 9:15 am ] | ||
Post subject: | Re: file io | ||
wtd wrote: CroAnte wrote: Change your do structure to:
Close, but not quite. You see, the "text" variable is scoped locally to the block in this case. Therefore the test, which is outside the block, is unaware of its existence. You're right. In my defense, it was Friday. ![]() The other option is to declare the text variable earlier in the code. Our teacher gets us to do it at the beginning of the main method.... |
Author: | Tony [ Wed Oct 18, 2006 10:00 am ] |
Post subject: | Re: file io |
CroAnte wrote: The other option is to declare the text variable earlier in the code. Our teacher gets us to do it at the beginning of the main method....
A variable should only have as much scope as it needs. I hope your teacher isn't telling you to declare all your variables as method wide, just because it appears easier. It's best you understand why you're doing something a certain way or another, and don't dismiss "just do it this way" lightly. You'll miss out, and quite possibly re-enforce bad practice from continues use, which is arguably more difficult to overcome than to learn a new concept from scratch. |