File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes use readLine() value outside loop Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "use readLine() value outside loop" Watch "use readLine() value outside loop" New topic
Author

use readLine() value outside loop

Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77


The while loop eventually executes whats declared inside its semicolons,so whenever you attempt to access 's' outside the loop you get a null value. But i want the actual value. Is there any way to achieve this? Thank you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Oceana Wickramasinghe wrote:

The while loop eventually executes whats declared inside its semicolons,


Braces, not semicolons.

so whenever you attempt to access 's' outside the loop you get a null value.


Not sure what cause/effect you're implying by "so" here, but the reason you get a null value outside the loop is because that's the last value that it was set to. If you stopped the loop before readLine() returned null, the println() after the loop would print the last line read.

But i want the actual value. Is there any way to achieve this? Thank you.


Which actual value? The s variable gets assigned as many values as there are lines in the file, and then gets assigned the value null, which is what you see after the loop. Null is just as much an "actual" value as any other.

What value do you want to see after the loop? What are you actually trying to accomplish?
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Whatever is written in the file im reading is what i consider to be the actual value. Now if i were to just print the value i could have used println(s) within the loop, but i want to read this value outside this loop. Something like this


Is there a way around this?
Oceana Wickramasinghe
Ranch Hand

Joined: Mar 02, 2011
Posts: 77
Okey, i broke the loop and it worked, thanks for your help.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Oceana Wickramasinghe wrote:Whatever is written in the file im reading is what i consider to be the actual value.


As I already said: There are many "actual" values.

If your file is:


Then s first gets the value of "abc", then it gets the value of "def", then it gets the value of "xyz", then it gets the value of null. So if you want to do something with the value of s, it has to be one of those, and you have to specify which one, and if you want it to be one of the non-null ones that occurs inside the loop, then you either need to use it while it has that value during the iteration, or you need to save it for later.

Now if i were to just print the value i could have used println(s) within the loop, but i want to read this value outside this loop. Something like this


Sounds like you either want to append to a StringBuilder each time through the loop, so that you can have a single String that has the entire contents of the file (as opposed to separate Strings for each line, which is what you have now), or you want to add each line to List as you read it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: use readLine() value outside loop
 
Similar Threads
FileIO
Loops in java
Selective retrieval of numbers in a text file
Loops using input
Scanner class