Forums Register Login
Getting String input from file
OK, this is supposed to be easy, still I could not find the answer in google or searching this sub-forum. Please help.

My problem in simple form, I have a text file that has two lines:


i have two variables in my program var1 and var2.

how do I get the values of those two lines, into my two variables?
so that var1 ="this/is/line/1" and var2 = "this/is/line/2"
thanks in advanced.
This is the most common way to read a file:

That while condition is rather complex the first time you see one like it. This part:

( line = input.readLine() )

reads one line of text into a variable called line. An assigment clause like that also has a value, which is the value of the variable just assigned. The second part of the while condition compares that to null. Null means we tried to read but we were at the end of file and got nothing.

You wanted to get two variables, line1 and line2. I didn't do that. This more general solution puts lines read into a collection or an array so it can handle any number of lines.

Did that help?
Thanks for reply. I'll try it out now, and will let you know if I am able to make it to work or not.

By the way, is 'input' a FileReader type?
Never mind, its BufferedReader. I got it to work. Thanks mate.
Excellent! We deliberately leave out details here ... it's much more fun if you figure them out yourself. Keep having fun!

This thread has been viewed 1045 times.

All times above are in ranch (not your local) time.
The current ranch time is
Dec 16, 2018 17:19:03.