#how to choose a specific line in a txt file to add to a string
1 messages · Page 1 of 1 (latest)
Helper please have a look, thanks.
Choose how? By line number or some other criteria?
by line number
Files.lines(Path) returns a Stream<String> of the lines in the file. You can skip the earlier lines and the take the first element. If the file were shorter, the result will be empty.
so would i write it as
String lineOutput = File.lines("file03.txt")
Note that the stream returned needs to be closed, so you should use it in a try-with-resources block.
No it returns a Stream<String> and the method takes a Path, not a String.
Path p = Path.of("file03.txt"); // note: assumes the curent folder
try (Stream<String> lines = Files.lines(p)) {
... process the stream here
}
ok so im trying to understand this
Do you know how to use the Stream class? Or has your learning not covered Stream yet?
If this is a course question it probably assumes you're only using the constructs taught up to this point (and with most courses that usually means old ways of doing things).
How do you think you would solve it, given the tools you've been taught
well its a lesson that i missed so im kindof self teaching it to myself right now
actually ill send a screenshot of my code
the next part of the question is to allow the user to overwrite data that they have entered
so i have to basically find which line they want to edit
then ask them for the info again
lmk what u think @wraith vortex
In that scenario you can't just overwrite a single line.
A file is just a sequence of bytes and the lines aren't all the same length.
You'd have to read and then rewrite the file, with just the one line different.
You could rewrite only from the changed line onwards (more complex and with dubious benefits), but you can't just change one line in the middle of the file.
In your case, you're keeping the file content in memory (in String[] info) so I'd move file writing out to a method that can be called to rewrite the file.
That is, instead of writing to the file in the input loop, write the info array to the file after that loop. This means you can just alter the array content and rewrite the file as often as you'd like.
so the better option would just be me rewriting the whole file
but i still havent found a way to search for a specific line in a text file
It's not that it's a better option, it's that you can't rewrite just a single line.
Consider a list of numbers, selecting from 1..999, written as their individual digits.
"1,73,657,12,42,467,7"
Can you replace the number 12 with 777 without changing the position in the string of 42?
no
another question that i have to answer is to allow the user to pick a line from a txt file of song lyrics and display that line
how would i do that
Files are the same. So if you replace a line, unless it's identical in length, you are rewriting the rest. In the general case, you're just better to rewrite the file.
Read the file one line at a time, until you get to the line number you want to display.
how do i do that
BufferedReader has a readLine() method.
ok perfect
and
how do i add a file to my code
im on a mac so idk the file name
like the specific directory
actually nvm
i know the directory
just dont know how to add the specific file
'add'? You mean create the file? Copy it there if it exists elsewhere or create it with an editor or shell command (or create it with code).
yea i got it
im using bufferedreader
and i still dont know how to check a specific line
Do you know how to do a loop with a counter? Do that, read a line at a time, until you get the nth line (or run out of lines in the file)