Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java error message is not displaying in appropriate place. In the voidmarkItemOn

ID: 3731459 • Letter: J

Question

Java error message is not displaying in appropriate place. In the voidmarkItemOnLoan portion of the code, the error message "Title is not part of your existing library" is not displaying after entering a title. It still prompts user to enter who you are loaning it to and when you are loaning it to them and then will display it. It needs to display after the title if non-existent.

import java.util.Scanner;

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();

System.out.println (" ");

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 + " is not part of your existing Library. ");

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

You have not entered " import java.lang.* " in headers (MediaItem is actually part of this).

Try below code:

import java.util.*;
import java.io.*;
import java.lang.*;


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();

System.out.println (" ");

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 + " is not part of your existing Library. ");

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;

}

}  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote