I have a text file I am importing data from that is information about items in a library. One of these pieces of information is the date the item was loaned. I cannot figure out what syntax I need to get the text from the file that will be in dd/MM/yyyy format read in and converted to a Date value using
import java.util.Date;
As it currently sits, the error is "cannot find symbol
symbol: method SimpleDateFormat(String) location: class digitalLibrary
I am not wanting it to look for a method I wrote... Instead I believe what I want it to do is use
import java.text.SimpleDateFormat;
Any help would be greatly appreciated
public static Item createLibrary (ArrayList <Item> library)
{
Scanner reader = null;
try {
File inputFile = new File("library.txt");
reader = new Scanner(inputFile);
} catch (FileNotFoundException ex) {
System.out.println("Could not open the file.");
System.exit(0);
}
//This loop fills the ArrayList with the input from the library.txt file
for (int i =0; reader.hasNext(); i++)
{
Item a = new Item();
a.setTitle(reader.nextLine());
a.setFormat(reader.nextLine());
a.setOnLoan(Boolean.parseBoolean(reader.nextLine()));
a.setLoanedTo(reader.nextLine());
//My problem is in the next two lines of code, specifically
//SimpleDateFormat
String dateFormat = "dd/MM/yyyy";
a.setDateLoaned(SimpleDateFormat(dateFormat).parse(reader.nextLine()));
reader.nextLine();
//This adds the item with all the attributes to the ArrayList library
library.add(a);
}
}
This is separate code I have written inside my Item class for when I ask the user for when the item was loaned out that is similar and works:
public Date getTheDateLoaned()
{
System.out.println("What day was it loaned out in dd/MM/yyyy format?");
String dateFormat = "dd/MM/yyyy";
Scanner scanner = new Scanner(System.in);
try
{
return new SimpleDateFormat(dateFormat).parse(scanner.nextLine());
}
catch (ParseException ex)
{
System.out.println("This was not in the right format. The format"
+ "needs to be dd/MM//yyyy");
}
return null;
}
Thank you again for any guidance!
Without seeing the entire exact printout of the error, I can't be sure, but it looks like you haven't imported SimpleDateFormat.
Before your class statement, add the line import java.text.SimpleDateFormat;
, and you should be all set. Did you try this?
Also, you forgot new
.
Try this:
a.setDateLoaned((new SimpleDateFormat(dateFormat)).parse(reader.nextLine()));