Question: Please see input code and media file after the error. When inputing a title, it won't display what's in the system and goes into an

Please see input code and media file after the error. When inputing a title, it won't display what's in the system and goes into an error as follows:

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 [EBook [id:10 title:To Kill a Mocking Bird chapter:31 year:1962 available:true] , MovieDVD [id:11 title:To Kill a Mocking Bird chapter:2 year:1962 available:false] ] Welcome to media rental system 1: Load Media objects 2: Find Media objects 3: Rent Media objects 9: exit Enter choice 2 Enter media title To Kill a Mocking Bird Welcome to media rental system 1: Load Media objects 2: Find Media objects 3: Rent Media objects 9: exit Exception in thread "main" Enter choice 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.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at EZRentals.Main.main(Main.java:13)

Input code:

Main.java

package EZRentals;

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\jahoo\Documents\Media.txt System.out.println("Enter file path load media "); String path=sc.next(); Manager.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;

public class Manager { static List list = new ArrayList<>();

public static 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.replaceAll("\\[", "").replaceAll("\\]","").split(" "); if (str[0].equals("EBook")) { list.add(new EBook(Integer.parseInt(str[1].substring(3,5)), str[2].substring(6) + " " + str[3] + " " +str[4] + " " + str [5] + " " + str[6].substring(0,4), Integer.parseInt(str[7].substring(5,9)), Integer.parseInt(str[8].substring(9,11)), Boolean.parseBoolean(str[9].substring(10)))); } else if (str[0].equals("MusicCD")) { list.add(new MusicCD(Integer.parseInt(str[1].substring(3,5)), str[2].substring(6) + " " + str[3] + " " +str[4] + " " + str [5] + " " + str[6].substring(0,4), Integer.parseInt(str[7].substring(5,9)), Integer.parseInt(str[8].substring(8,9)), Boolean.parseBoolean(str[10]))); } else { list.add(new MovieDVD(Integer.parseInt(str[1].substring(3,5)), str[2].substring(6) + " " + str[3] + " " +str[4] + " " + str [5] + " " + str[6].substring(0,4), Integer.parseInt(str[7].substring(5,9)), Integer.parseInt(str[8].substring(8,9)), Boolean.parseBoolean(str[10]))); } } 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.println(m);

} }

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 "); } } }

}

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; }

}

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 + "] "; }

}

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 + "] "; } }

Media.txt: 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]

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!