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 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.