Java assignment - This is the second class code for a personal lending library.
ID: 3730329 • Letter: J
Question
Java assignment - This is the second class code for a personal lending library. I am getting errors on the scanner. Do I need to have the import java.util.Scanner with the library class as well as the main code?
Here is the code for the second class.
public class Library {
static Scanner in = new Scanner(System.in); //instantiation
MediaItem t = new MediaItem();
MediaItem[] items = new MediaItem[100];
String[] str = new String[100];
int numberOfItems = 0; //fields
int check = 0;
int called = 0;
int displayMenu(){ //methods
System.out.println("1. Add new item 2. Mark an item as on loan 3. List all items 4. Mark an item as returned 5. Quit");
System.out.print("What would you like to do? ");
int a = in.nextInt();
return a;
}
void addNewItem(String title, String format){
MediaItem item = new MediaItem(title, format);
items[numberOfItems] = item;
numberOfItems++;
}
void markItemOnLoan(String title, String name, String date){
for(int b = 0; b < numberOfItems; b++){
if(title.equals(items[b].title)){
items[b].markOnLoan(name, date);
called = 1;
}
}
if(called == 0)
System.out.println(title + " doesn't exist");
called = 0;
}
void listAllItems(){
for(int c = 0; c < numberOfItems; c++){
if (items[c].onLoan)
str[c] = " " + items[c].title + " " + items[c].format + " loaned to " + items[c].loanedTo + " on " + items[c].dateLoaned;
else
str[c] = " " + items[c].title + " " + items[c].format;
System.out.println(str[c] + " ");
}
}
void markItemReturned(String title){
for(int b = 0; b < numberOfItems; b++){
if(title.equals(items[b].title)){
items[b].markReturned();
check = 1;
}
}
if(check == 0)
System.out.println("Sorry, I couldn't find " + title + " in the library.");
check = 0;
}
}
Explanation / Answer
Yes, add
import java.util.Scanner;
in all the classes when Scanner object is created.
there is no error beside this except the MediaItem class which is not provided in the question.
Do ask if there is any other query in the question.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.