Reputation:
I am creating a large array, where each element is the object Temperature(String dayMonthYear, String hour, double degrees, double speed)
I then take the dayMonthYear and hour and try to parse it into a date which is stored as an instance along with the degrees and speed (of wind).
Below is the code for making the array, the 86793 is the number of lines in the file
public Temperature[] readTemperatures(String filename) {
Temperature[] temp = new Temperature[86793];
File f = new File(filename);
Scanner scan;
int i = 0;
try {
scan = new Scanner(f);
while(scan.hasNextLine()) {
String dayMonthYear = scan.next();
String hour = scan.next();
double degrees = scan.nextDouble();
double speed = scan.nextDouble();
temp[i] = new Temperature(dayMonthYear, hour, degrees, speed);
i++;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return temp;
}
Below is the contractor and the date parser
public Temperature(String dayMonthYear, String hour, double degrees, double speed) {
Date date = createDate(dayMonthYear, hour);
this.date = date;
this.temperature = degrees;
this.windspeed = speed;
}
// Method to create date
public static Date createDate(String date, String hour) {
Date returnDate = null;
SimpleDateFormat formatter = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
String stringDate = date + " " + hour;
try {
returnDate = formatter.parse(stringDate);
} catch (Exception e) {
System.out.println("Invalid format: " + stringDate);
}
return returnDate;
}
Below is how the data I am trying to input looks like 1-Jan-2006 0:00 41.4 2
1-Jan-2006 1:00 39.8 1.9
[I have tried changing the inputs to something like
01-Jan-2006 03:00 41.5 1.7
but no avail]
The errors I get are
Invalid format: 01-Jan-2006 00:00
Exception in thread "main" java.util.InputMismatchException
at java.base/java.util.Scanner.throwFor(Scanner.java:939)
at java.base/java.util.Scanner.next(Scanner.java:1594)
at java.base/java.util.Scanner.nextDouble(Scanner.java:2564)
at P10.readTemperatures(P10.java:22)
at P10.main(P10.java:61)
Upvotes: 0
Views: 85
Reputation: 347204
This "may" or "may not" be directly related to the issue you are having, but it should serve as warning...
nb This could also be the way my system is configured, but it took me by surprise
There seems to be a change in how the MMM
specifier works (at least in Java 10). For example, if I do something like...
Calendar cal = Calendar.getInstance();
cal.set(Calendar.MONTH, Calendar.JANUARY);
cal.set(Calendar.DATE, 1);
cal.set(Calendar.YEAR, 2066);
SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy");
System.out.println(sdf.format(cal.getTime()));
It will print 01-Jan.-2066
... Notice the .
at the end of the month.
This is probably a good opportunity to move away from, what is essentially a deprecated API and embrace the new date/time classes available since Java 8 (or the 3/10 backport)
So, instead, you might be able to do something like...
String date = "01-Jan-2006";
String hour = "00:00";
LocalDate ld = LocalDate.parse(date, DateTimeFormatter.ofPattern("dd-MMM-yyyy", Locale.UK));
LocalTime lt = LocalTime.parse(hour, DateTimeFormatter.ISO_LOCAL_TIME.localizedBy(Locale.UK));
LocalDateTime ldt = LocalDateTime.of(ld, lt);
System.out.println(ldt);
which will print 2006-01-01T00:00
nb someone will point out that you don't "need" to do two separated conversations and you "could" concatenate the date
/hour
and do it in one step, which you "could" do, but since you had the values separated anyway, it just made sense to treat them separately
Upvotes: 1
Reputation: 509
Set correct locale for the scanner using useLocale(Locale.US) function from Scanner. It is possible that you are using a locale where decimal delimiter is not a '.' but some other character like comma.
Upvotes: 0