wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem involving Arraylists  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem involving Arraylists  " Watch "Problem involving Arraylists  " New topic
Author

Problem involving Arraylists

Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
So I have been stuck for a while, cant seem to get what to do.

introduce a Bin class. Each object of this class represents
a bin in the warehouse. It has two instance variables: a String that contains
the bin name and an ArrayList<BinItem> that contains a BinItem for each
of the SKUs stored in the bin. It also has a method making it possible for us
to add a new SKU to the bin. Implement the Bin class by completing the definition.

Here' what I had, think my Arraylist<BinItem> is incomplete and incorrect so...

Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

Hi Noah, welcome to JavaRanch.

In the future, please UseCodeTags when posting code. I've added them for you this time.

In the getContents() method, can you describe in words what you want it to do? I think you've got the right idea there, but several details are off.
Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
Thanks for the response. I don't really understand what your asking, sorry I'm kind of a newbie to JAVA, just starting it as a freshman in high school. The main problem is I also don't know how to put the correct syntax in the add(BinItem B), my main problem.
Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
Oh wait I get what you mean, it needs to contain a BinItem for each
of the SKUs stored in the bin. It also has a method making it possible for us
to add a new SKU to the bin. ( for the getContents method)

Sorry for the late response.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

You haven't told us what a BinItem is. It seems to relate to a SKU, but how exactly? Leaving that aside for now, it seems to me the most obvious way to implement getContents() would be to return myContents. Instead you have a loop. I can think of a reason why you might want a loop there, but I'm curious to know your reason.

New programers tend to get too wrapped up by syntax. You need to be able to state in plain English the steps you need to follow to accomplish each task. Take your loop. In English, it might read: for each integer in the bin name, add the integer to list. Are there integers in the String myName? If there were, would you want to add them to list? What is list anyway? That's the only time in the code it's even mentioned.

Figure out what you want to do first, and then worry about how to code it.
Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
I see, BinItem is are all the items in a bin that share the same SKU. SKU is the code for the song. (This code is databasing a library of songs) Also regarding the loop, I actually meant just to loop it I just added the previous syntax because the textbook example had one so i thought it was necessary. It also has to contain a binitem for every SKU that is added to bin.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2849
    
  11

I still don't understand BinItem. What data does it contain? SKU seems to be one piece, and also maybe a collection of ... what? Maybe it's a list of songs that share the same code. You also call them items though. Are you using songs and items to mean the same thing?

I can't parse the meaning out of this sentence:

Noah Kim wrote:I actually meant just to loop it I just added the previous syntax because the textbook example had one so i thought it was necessary.


It sounds like you are cutting-and-pasting code that you don't understand. I know from long, painful experience that's unlikely to work! It's not impossible, mind you, but unlikely. Your first step should be to understand the code you are copying. After that making it work for your case will be much easier.
Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
Yeah... hehehe. I think I worked it out.


import java.util.*;
public class Bin
{

private String myName;
private ArrayList<BinItem> myContents;
public Bin( String name )
{
myName=name;
}
public String getName()
{
return myName;
}
public ArrayList<BinItem> getContents()
{
return myContents;
}

public void add( BinItem b )
{
myContents.add( b );
}
public String toString()
{
String s = "Bin " + myName + ":\n";

for ( BinItem b : myContents )
s += b + "\n";
return s;
}
}

Noah Kim
Greenhorn

Joined: Feb 13, 2013
Posts: 6
Wait I got it, nevermind. Thanks for the help sir.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Noah Kim wrote:. . . I think I worked it out. . . .
And I don’t.
You don’t appear to be using the List.
You would find out if you had used the List because of the NullPointerException.
You have some bad practice in the toString method. Avoid using the + operator on Strings in multiple statements. Use a StringBuilder instead. Despite what it shows in many books, you should avoid the \n and \r escape sequences. One solution is to use String.format and the %n tag.

You didn’t use the code tags correctly; it says here how they work. But since your code was not indented, they would have made little difference.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem involving Arraylists