• Post Reply Bookmark Topic Watch Topic
  • New Topic

problem with my media library program  RSS feed

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
line 38 I am getting the error Type mismatch: cannot convert from String to MediaItem

what I am trying to do here is input a new media item into my item array.

Can I get some help with the proper way of doing this?



 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:line 38 I am getting the error Type mismatch: cannot convert from String to MediaItem

what I am trying to do here is input a new media item into my item array.

Can I get some help with the proper way of doing this?


"nextLine() returns a String an item is expecting a MediaItem. The usual way to perform this conversion is with a constructor, specifically, a MediaItem constructor. So then you'd haave
Now you have to create this constructor in your MediaItem class, and it should take a single String parameter which the constructor needs to convert to a MediaItem.

Also, your variable 'i' is overshadowing the variable you already have: "numberOfItems". Do away with 'i' and replace it with 'numberOfItems'.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That did the trick, any suggestions on how I could input the type of media the item is into the same array? For example, say I input Iron man, and it is blu ray, how would I store that to where it is paired with the iron man media item
[code
// Add new item method
public void addNewItem(String title, String format){
System.out.print("What is the title");
item[numberOfItems] = new MediaItem( input.nextLine() );
System.out.print("What is the format");
format = input.nextLine();
numberOfItems++;
}
[/code]
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can add the format also to MediaItem, just like you did for the title.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean just like I did the title? I did the title like this

item[numberOfItems] = new MediaItem( input.nextLine() );

can I do the format like

title[numberOfItems] = new MediaItem( input.nextLine() );
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I meant can I do the format like this
format[numberOfItems] = new MediaItem( input.nextLine() );
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

And then add the 'String format' also to your MediaItem class
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where is it that segment of code is to go?  if I put it where I input my title, here


then I will never get a prompt to input format until after I have input my format

I also don't know how to format my MediaItem class to string

 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still no clue on the previous question, here is where I am at


 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to be able to add the type of media that the item is to the array, as well as verify whether the program is on loan or not. No idea how to even begin
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is another update, 
line 44 error:  The method markItemReturned() in the type Library is not applicable for the arguments (String)
I have not been able to run program due to this error, can you tell if this is properly done? I have no idea how else to adjust these arrays with the name value, whether it is onLoan or not, etc...


 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone know of any way this is to be done?
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would start by looking at your design so far. You have your main method in class MediaItem. Isn't this weird? You can have several MediaItems in a Library.
I would start with something like this:
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yea I still don't know how it is to work, I can have all the exercises in the world but none will help if I am just staring at my screen for hours wondering how to connect all of these things into one array variable.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think I will just skip this assignment, as it makes no sense to me, and I see no example codes of anything of the sort online, to help me understand how this works.
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you skip this assigment because you don't understand how to solve it, then you probably face the same problem again with a next assignment...
Is the assigment not clear to you? Can you share the assigment with us?
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know, that is what usually happens, here is the assignment. I simply don't understand how I can create an array that stores all of media titles, and in the same memory space, store the name of who it was rented to, as well as the Boolean if it is currently rented out, and the date it was rented out. I code, and then I get so far, and get totally lost in my code, and do not know how to pass and access all of these things. 
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Nevermind, I could not import my pdf file, I am at work at the moment, I will upload it to you once I am off later today, thanks
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, here are the instructions.


Description
The goal of this project is to create a “personal lending library” tool.  The user wants to keep track of
their movies and games – which ones they own, whether or not they are currently loaned out to anyone,
and if so, who they were loaned to and on what date.  For each item in the library, the program should
know its title and format.  For a movie, the format is BlueRay or DVD.  For a game, the format is the
platform the game runs on, such as Windows, Mac, XBox, Playstation, etc.  The program should be
capable of storing up to 100 items in the library.  Right now our library will be wiped when the
program terminates, but in the next half of the class we will learn how to make the information stick
around between program executions.
The program should be capable of the following actions:

Adding a new item to the library

Marking an item in the library as on loan

Listing all of the items in the library (title and format and, if it is currently on loan, the person it
is loaned to and the date of the loan)

Marking an item as returned
Sample Run
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 1
What is the title?
Star Wars
What is the format?
DVD
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 1
What is the title?
Bioshock Infinite
What is the format?
XBox 360
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 2
Which item (enter the title)?
Aliens
I'm sorry, I couldn't find Aliens in the library.
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 2
Which item (enter the title)?
Bioshock Infinite
Who are you loaning it to?
Mike
When did you loan it to them?
April 2nd
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 2
Which item (enter the title)?
Bioshock Infinite
Who are you loaning it to?
James
When did you loan it to them?
April 5th
Bioshock Infinite is already on loan to Mike
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 3
Star Wars (DVD)
Bioshock Infinite (XBox 360) loaned to Mike on April 2nd
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 4
Which item (enter the title)?
Star Wars
Star Wars is not currently on loan
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 4
Which item (enter the title)?
Aliens
I'm sorry, I couldn't find Aliens in the library.
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 4
Which item (enter the title)?
Bioshock Infinite
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 3
Star Wars (DVD)
Bioshock Infinite (XBox 360)
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 5
Goodbye!
Suggestions
You have freedom to design your program however you want, provided that it meets the requirements
and follows good design principles.  However, if you would like some ideas of where to start, they are
provided in this section.
This program lends itself well to having two classes, MediaItem and Library, with the fields and
methods described below.
MediaItem
fields:

String title

String format

boolean onLoan

String loanedTo

String dateLoaned
methods:

MediaItem()
– Constructor to initialize the fields of this media item to default values (null for
Strings and false for booleans)

MediaItem(String title, String format)
– Constructor to initialize the title and format of this
media item.  onLoan should be initialized to false.

getter and setter methods
for all class fields (title, format, onLoan, loanedTo, dateLoaned)

void markOnLoan(String name, String date)
– Sets onLoan to true and sets the loanedTo and
dateLoaned fields to the parameter values.  If onLoan is already true, print an error message
saying this item is already loaned out.

void markReturned()
– Sets onLoan to false.  If onLoan was already false, print an error
message saying this item is not currently loaned out.
Library
fields:

MediaItem[] items
– An array to hold all of the items in the library.  This needs to be big
enough to hold 100 items.

int numberOfItems
– The number of items actually stored in the array.  This needs to be
incremented whenever a new item is added.
methods:

int displayMenu()
– Show the menu of options to the user and read in their choice.  Repeat until
the user enters a valid option.  Then return the option they chose.

void addNewItem(String title, String format)
– Create the new MediaItem object, add it to the
items array, and increment the numberOfItems variable by one.

void markItemOnLoan(String title, String name, String date)
– Iterate through the items array
and find the item with the correct title.  Call that item's markOnLoan method.  If you cannot
find the correct item in the array, display an error message.

String[] listAllItems()
– Create a String array big enough to hold all of the items in the items
array.  Iterate through the items array, up to the numberOfItems that array contains.  For each
item, create a String containing its title and format.  If the item is on loan, also include in the
string who the item was loaned to and when.  Add this string to the String array.  When you
have converted all of the items to strings, return the String array. 

void markItemReturned(String title)
– Iterate through the items array and find the item with
the correct title.  Call that item's markReturned method.  If you cannot find the correct item in
the array, display an error message.

public static void main(String[] args)
– The main method will drive your Library program by
repeatedly displaying the menu to the user, prompting the user for any required information,
and calling the appropriate method.  Similarly, if the method returns information, this data
should be displayed from within the main method rather than the Library method.  For instance,
if the user chooses option 2 (mark an item as on loan), your main method should prompt the
user for the title of the item, the name of the person it was loaned to, and the date on which it
was loaned, and then call the Library class's markItemOnLoan method with these values.  You
should not have the markItemOnLoan method get the input from the user directly, because that
locks your code into a particular interface (text input).
Hint
If you have the following problem, where your program does not wait for you to type in the title before
asking you for the format, please read this section.
1. Add new item
2. Mark an item as on loan
3. List all items
4. Mark an item as returned
5. Quit
What would you like to do? 1
What is the title? What is the format?
The problem here occurs if you are using the same Scanner object throughout your program rather than
a new one in each method.  There is nothing wrong with this (in fact, it is slightly more efficient), but it
can get a little quirky.  When you read in a number using a Scanner, as the displayMenu method does,
Java reads in the number but not the <ENTER> that the user types after the number.  So when you later
use the nextLine() method to read in the title, Java sees that <ENTER> still out there from before, and
reads it in and assigns it to the title variable.  To prevent this, right after you call the Scanner's nextInt()
method in your displayMenu method, add this line to skip over the <ENTER> at the end:
myScanner.nextLine();
Rubric
  (Each item is worth
three
points, for a total of 39)

The program displays the appropriate menu to the user and prompts them for input (checking
for invalid input)

The program allows the user to add a new item

The program allows the user to mark an item as on loan to a particular person on a particular
date

If the user tries to mark an item on loan that is currently loaned out, an error message is
displayed

If the user tries to mark an item on loan that does not exist, an error message is displayed

The program can list the titles and formats of all items in the library

If an item is loaned out, the program includes the person it is loaned to and the date on which it
was loaned when listing the item

The program allows the user to mark an item as returned

If the user tries to mark an item that is not currently loaned out as returned, an error message is
displayed

If the user tries to mark a non-existent item as returned, an error message is displayed

The program compiles

The program follows good coding conventions and design guidelines

The program runs
 
Indigo Montes
Greenhorn
Posts: 22
1
C++ IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a lot of text
But the advantage of this is that you see all the small steps to build the program. Which also means that you can check every small step you made.
You could start by using the already given code. Try to make it work step by step.

You have freedom to design your program however you want, provided that it meets the requirements
and follows good design principles. 

First meet the requirements, then improve some things if you want to.

You said before:

I simply don't understand how I can create an array that stores all of media titles, and in the same memory space, store the name of who it was rented to, as well as the Boolean if it is currently rented out, and the date it was rented out. I code, and then I get so far, and get totally lost in my code, and do not know how to pass and access all of these things.

Creating the array isn't the problem. Forget about the array and filling it with all those fields. Because they belong in the MediaItem class. Take a look at the given example code.
So you have to read the user input and store it in their proper fields. Then give them to the MediaItem class, so that it contains all the data. Then add this new MediaItem to your array.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the late reply, I am getting started again on this now, and will post you the code when finished.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is my code thus far, I think the majority of my help needed is in the library class. I have left comments on some of the methods I do not understand. I will keep working on it and post another update if I get more figured out prior to any response.





 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See Creating Objects
Google is your friend. Sometimes rolling up your sleeves and doing some research is what's needed.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, and that is what I have been doing for the past 2 weeks on this assignment, yet I still have no idea how to send multiple values of different types to one single array, and how to receive those values back, and change them. I don't see how that is such a hard question to get an answer to.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
perhaps I am allowed an answer on this

I go to run the program, if I choose menu option 1 (to add new item) it makes me input the title and format twice.
also, when the program outputs my title back to me, it is in hash form for some reason.





 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:I go to run the program, if I choose menu option 1 (to add new item) it makes me input the title and format twice.

The case blocks in your switch() are missing "break".
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The "title" variable is local in scope. It may have the same name as the member variable but it's not.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:also, when the program outputs my title back to me, it is in hash form for some reason.

The hash is the default that comes from inheriting from Object unless your MediaItem overrides the toString() method, which you have not done.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have fixed everything I do believe, besides the toString method, which would be the best route to go about this? As everything I try I receive errors

1st way found online


2nd way found online


etc...
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That would allow you to print the MediaItem like this
However, it appears that all you want to do is print the title, and in that case it would be
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kennith stomps wrote:2nd way found online

I don't believe you found this online. It won't compile let alone work. The format() method is very powerful and useful but its use requires some careful study and probably some experimentation.
 
kennith stomps
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, I have gotten that, however with that being said, when I list all items, I am still getting the hash output, although everything seems to be correct, in regards to how I have output the other strings.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:That would allow you to print the MediaItem like this
However, it appears that all you want to do is print the title, and in that case it would be

Read the beginning of this again.
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a tutorial on toString.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!