I am unable to figure out why I am getting the DateTimeParseException error when the text I am passing through fits the format. Below is the code that causes the issue:
LocalTime lt = LocalTime.parse(songTime,
DateTimeFormatter.ofPattern("k:m:s"));
Here is the weird thing. Whenever I query the user for a time (let's use 00:02:30 as an example), it runs exactly as I want. But when I use my method (which extracts the time from a text file) it gives the error:
Exception in thread "main" java.time.format.DateTimeParseException: Text '00:02:30' could not be parsed, unparsed text found at index 8
The first thing I assumed, was that it might of been bringing in a extra whitespace or something along the lines of that. So to check this, I printed 3 lines on each side of the variable and it printed this:
---00:02:30---
As you can see above, there were no white spaces. If I hardcode 00:02:30 then it works perfectly as well. I then ran into another thing that bugged me. My text file looks like this:
00:00:00 First
00:02:30 Second
The first time passes perfectly but anyone after that causes the error. They both have the exact same format with no whitespace on either side so I fail to see the problem. I checked every single forum post on the issue and majority of them were the individuals using the wrong formats, wrong strings etc. I am not sure that is the case here as it works perfectly when I hardcode it or query the user for input.
Below are the meanings of each option I chose in the Formatter (from the documentation):
k clock-hour-of-am-pm (1-24)
m minute-of-hour
s second-of-minute
Here is the method that reads the file:
public static ArrayList<Song> scanInSongs () {
ArrayList<Song> songArray = new ArrayList<Song>();
try {
BufferedReader reader = new BufferedReader(new FileReader("Description.txt"));
String line;
while ((line = reader.readLine()) != null) {
String key = line.substring(0, line.indexOf(' '));
System.out.println("Fetched timestamp: "+ key);
String value = line.substring(line.indexOf(' ') + 1);
System.out.println("Fetched name: "+ value);
Song song = new Song(value, "", key);
songArray.add(song);
}
} catch (IOException e) {
System.out.println("File not found, exception: "+ e);
}
return songArray;
}
The Song Class:
public class Song {
private String duration = "";
private String name = "";
private String timestampFromVideo = "";
public Song(String name, String timestampFromVideo, String duration) {
if (name == "") {
this.name = "";
} else {
this.name = name;
}
this.duration = duration;
this.timestampFromVideo = timestampFromVideo;
}
public String getName() {
return this.name;
}
public String getDuration() {
return this.duration;
}
public String getTimestampFromVideo() {
return this.timestampFromVideo;
}
public void setDuration(String duration) {
this.duration = duration;
}
}
The main:
public static void main(String[] args) {
ArrayList<Song> songArray = scanInSongs();
String songTime = songArray.get(0).getDuration();
LocalTime lt = LocalTime.parse(songTime,
DateTimeFormatter.ofPattern("k:m:s"));
}
Lastly as stated before is the file:
00:00:00 First
00:02:30 Second
Thank you in advance for the help everyone!