I need to read in a text file (catalog.txt) that has these entries: \"Terminator
ID: 3531949 • Letter: I
Question
I need to read in a text file (catalog.txt) that has these entries:
"Terminator" 1984 A1
"Argo" 2012 B2
"The Wizard of Oz" 1940 C5
"Howard the Duck" 1985 D9
PS: How else would the third entries be read as? The One Letter One Number ones. I notice they do not have quotation marks so I assume they aren't Strings.
public class DVDCatalog {
public static void main(String[] args) {
ArrayList<Video> videos = new ArrayList<Video>();
BufferedReader file = new BufferedReader (new FileReader ("catalog.txt"));
// Video currentVideo;
int response = 0;
// A couple of default dvds
videos.add(new Video("Terminator 2: Judgement Day", 1991, "23342353"));
videos.add(new Video ("Titanic", 1997, "58456721"));
//Do while loop to continue the process as long as the responses are answered
do {
System.out.println("Welcome to your DVD Collection!");
System.out.println("Choose your option now:");
System.out.println("1.) Add a DVD to your collection");
System.out.println("2.) View your current collection");
System.out.println("3.) Remove a DVD from your collection");
System.out.println("Please specify your choice with either 1, 2, or 3");
System.out.println("When you are finished, type any other response.");
//Create the scanner and allow them to answer
Scanner input = new Scanner(System.in);
response = input.nextInt();
input.nextLine();
//If they want to add a video
if (response == 1) {
// Adding video
System.out.println("Please enter in the title of your DVD: ");
String title = input.nextLine();
System.out.println("Please enter in the release date of the DVD: ");
int releaseDate = input.nextInt();
System.out.println("Please enter in the catalog code for the DVD: ");
String code = input.nextLine();
System.out.println(" ");
// Create the object dvd to store this data
Video dvd = new Video(title, releaseDate, code);
videos.add(dvd);
}
//Generate the list to view current objects stored in the array
else if (response == 2) {
// view collection of DVDs
System.out.println("This is your current list of DVDs: ");
for (int i = 0; i < videos.size(); i++) {
Video dvd = videos.get(i);
System.out.println(i + ".)" + dvd);
}
}
//Deleting a video
else if (response == 3) {
System.out.println("Please enter number of entry you would like to delete: ");
for (int i = 0; i < videos.size(); i++) {
Video dvd = videos.get(i);
System.out.println(i + ".)" + dvd);
}
int choice = input.nextInt();
videos.remove(choice);
}
//The while statement staying as long as it's within the boundaries of 1 through 3
} while (response >= 1 && response <= 3);
}
}
Explanation / Answer
/* use the code below for reading the file but one thing you should know that this code works only if there is only one space between each field(title,releaseDate,code) and the title field is surrounded by " title" as you have shown in the example , place the code below on your class to read file*/
try
{
String title;
String code;
int releaseDate;
String data[];
String line;
BufferedReader file = new BufferedReader (new FileReader ("C:\hello1.txt"));
BufferedReader f = new BufferedReader(file);
while((line=f.readLine())!=null)
{
data=line.split(""");
title=data[1];
data=data[2].split(" ");
releaseDate=Integer.parseInt(data[1]);
code=data[2];
}
}catch(FileNotFoundException ex)
{
}
catch(IOException ex)
{
}