a quick question I have the url of a text file on the web -
1) some lines begin with # - I need to ignore these - other lines have nunmbers and some useful information on them 1232432 21 #
Thus this is what the file looks like (with several entries)
# this is a sample file only to show you the format
# lines like this need to be ignored
12343453533 23 # 1 Jan 2012
12232324223 33 # 2 Jan 2013
12343434344 44 # 7 Jun 2016
# and more lines to be ignored
I only need the two numbers ie 12343453533 23, and 12232324223 33 and 12343434344 44 - how do I a) open the text file whose url I have and b) extract these numbers ? ie first and second column of the non # rows ?
Sorry for being late, but what is “so” in line 6? I hope it isn't a String. No, I don't think Strings have add methods; maybe it is a List, which is better.
Don't write an if block all on one line; indent it correctly.
Why are you not using a Scanner for a text file?
Why are you declaring so many exception types?
If you are using a buffered reader, are you familiar with this idiom:-
I would suggest that you can use a Stream nowadays;Please check that I have got the arguments for String#split right; I meant to use a regex comprising a hash sign but escaped, and divide once only, so the right half of the String becomes element 1 of the array. I suspect you will suffer an out of bound exceptions whichever way you try to split if the String doesn't contain a has sign.
The BufferedReader#lines() method creates a Stream<String> which handles each String returned from the reader in order. It first maps each String to something else, which happens to be another String, using the well‑known String#split method and keeping the right half of the text (as unnamedArray), using a regex which I have probably written wrongly.
It just happens that in this case we are mapping from a String to another String; in other cases you can map to a different type. So the second line creates a second Stream<String>, which can be collected into a List with the collect() method←examples of usage in that link. You will notice that takes a Collector as its argument, but you may have guessed from the examples that you can use the Collectors utility class. Yes, it has a method which returns a Collector creating a List.
All of the world's problems can be solved in a garden - Geoff Lawton. Tiny ad:
RavenDB is an Open Source NoSQL Database that’s fully transactional (ACID) across your database