Reputation: 17
I decided to create a currency converter in Java, and have it so that it would pull the conversion values out of a text file (to allow for easy editability since these values are constantly changing). I did manage to do it by using the Scanner class and putting all the values into an ArrayList.
Now I'm wondering if there is a way to add comments to the text file for the user to read, which Scanner will ignore. "//" doesn't seem to work.
Thanks
Upvotes: 0
Views: 329
Reputation: 1
With Java nio, you could do something like this. Assuming you want to ignore lines that start with "//" and end up with an ArrayList.
List<String> dataList;
Path path = FileSystems.getDefault().getPath(".", "data.txt");
dataList = Files.lines(path)
.filter(line -> !(line.startsWith("//")))
.collect(Collectors.toCollection(ArrayList::new));
Upvotes: 0
Reputation: 19
Yea, while ((currentLine = bufferedReader.readLine()) != null) is possibly the easiest, then perform your necessary tests. currentLine.split(regex) is also very handy for converting a line into an array of values using a delimiter.
Upvotes: 0
Reputation: 57998
Scanner wont ignore anything, you will have to remove the comments from your data after you have read it in.
Upvotes: 1
Reputation: 1109745
Best way would be to read the file line by line using java.io.BufferedReader
and scan every line for comments using String#startsWith()
where in you searches for "//"
.
But have you considered using a properties file and manage it using the java.util.Properties
API? This way you can benefit from a ready-made specification and API's and you can use # as start of comment line. Also see the tutorial at sun.com.
Upvotes: 11