Question
I'm running the following program with this file structure C:UsersjahooDocumentsMedia.txt and keep getting the below error. The information in the media file is this: EBook
I'm running the following program with this file structure C:\Users\jahoo\Documents\Media.txt and keep getting the below error.
The information in the media file is this: EBook [id=10, title=To Kill a Mocking Bird, Year=1962, Chapters=31, available=true] MovieDVD [id=11, title=To Kill a Mocking Bird, Year=1962, Runtime=2Hr9Min, available false]
I should be getting File cannot be opened: Could not load, no such directory when the file isn't there and no errors when the file is there. Can someone help show me what I'm doing wrong?
Error code: Welcome to media rental system 1: Load Media objects 2: Find Media objects 3: Rent Media objects 9: exit Enter choice 1 Enter file path load media C:\Users\jahoo\Documents\Media.txt Exception in thread "main" java.lang.NumberFormatException: For input string: "[id=10," at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67) at java.base/java.lang.Integer.parseInt(Integer.java:668) at java.base/java.lang.Integer.parseInt(Integer.java:786) at EZRentals.Manager.LoadMedia(Manager.java:24) at EZRentals.Main.main(Main.java:19)
Main.java
import java.util.Scanner;
public class Main {
public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (Menu()) { int choice = 0; System.out.println("Enter choice"); choice = sc.nextInt(); Manager m = new Manager(); switch (choice) { case 1: //String path = "C:\\Users\\Admin\\Documents\\workspace-spring-tool-suite-4-4.9.0.RELEASE\\Core-Java\\src\\media.txt"; System.out.println("Enter file path load media "); String path=sc.next(); m.LoadMedia(path); break;
case 2: System.out.println("Enter media title "); String title=sc.next(); m.findMedia(title); break;
case 3: System.out.println("Enter media id :"); int id =sc.nextInt(); m.rentMedia(id); break;
case 9: System.exit(0); break;
default: System.out.println("Enter valid choice "); break; } } sc.close(); }
private static boolean Menu() { System.out.println("Welcome to media rental system"); System.out.println("1: Load Media objects "); System.out.println("2: Find Media objects "); System.out.println("3: Rent Media objects "); System.out.println("9: exit "); return true; } }
Manager.java
package EZRentals;
import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.List; import java.util.Scanner;
class Manager {
static List
Manager() { }
public boolean LoadMedia(String path) { try { File myObj = new File(path); Scanner myReader = new Scanner(myObj); while (myReader.hasNextLine()) { String data = myReader.nextLine(); String[] str = data.split(" "); if (str[0].equals("EBook")) { list.add(new EBook(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]), Boolean.parseBoolean(str[5]))); }else if(str[0].equals("MusicCD")) { list.add(new MusicCD(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]), Boolean.parseBoolean(str[5]))); }else { list.add(new MovieDVD(Integer.parseInt(str[1]), str[2], Integer.parseInt(str[3]), Integer.parseInt(str[4]), Boolean.parseBoolean(str[5]))); } } System.out.println(list); myReader.close(); return true ; } catch (FileNotFoundException e) { System.out.println("An error occurred."); e.printStackTrace(); return false; } } public void findMedia(String title) { for(Media m : list) { if(m.getTitle().equals(title)) System.out.print(m.toString()); } } public void rentMedia(int id) { for(Media m : list ) { if(m.getId()==id) { if(m.isAvailable()) System.out.println("media successfully rented out "); else System.out.println("Media with id="+id+" is not available for rent "); } } }
}
EBook.java package EZRentals;
class EBook extends Media {
EBook(int id, String title, int year, int chapter, boolean available) { super(id, title, year, chapter, available); }
@Override public String toString() { return "EBook [id:" + this.id + " title:" + this.title + " chapter:" + this.chapter + " year:" + this.year + " available:" + this.available + "] "; }
}
Media.java package EZRentals;
class Media {
int id; String title; int year, chapter; boolean available;
Media() { }
public Media(int id, String title, int year, int chapter, boolean available) { this.id = id; this.title = title; this.year = year; this.chapter = chapter; this.available = available; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
public String getTitle() { return title; }
public void setTitle(String title) { this.title = title; }
public int getYear() { return year; }
public void setYear(int year) { this.year = year; }
public int getChapter() { return chapter; }
public void setChapter(int chapter) { this.chapter = chapter; }
public boolean isAvailable() { return available; }
public void setAvailable(boolean available) { this.available = available; }
}
MusicCD.java
package EZRentals;
class MusicCD extends Media {
MusicCD(int id, String title, int year, int chapter, boolean available) { super(id, title, year, chapter, available); }
@Override public String toString() { return "MusicCD [id:" + this.id + " title:" + this.title + " chapter:" + this.chapter + " year:" + this.year + " available:" + this.available + "] "; }
}
MovieDVD.java
package EZRentals;
class MovieDVD extends Media {
MovieDVD(int id, String title, int year, int chapter, boolean available) { super(id, title, year, chapter, available); }
@Override public String toString() { return "MovieDVD [id:" + this.id + " title:" + this.title + " chapter:" + this.chapter + " year:" + this.year + " available:" + this.available + "] "; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started