• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

use readLine() value outside loop

 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okey, i broke the loop and it worked, thanks for your help.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
This tiny ad is guaranteed to be gluten free.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic