0

I am have trouble creating an array or object(with multiple fields) and sending it to an array-list. Any help would be greatly appreciated. I have spent hours looking through every video on YouTube with the words object and array-list in them and have been unable to find much help.

The program needs to prompt the user to pick a option (1. AddItem) then prompt the user for the name and format (dvd, vhs) and save multiple objects with these variables in an array-list. I either keep having the location where it is saved in memory returned to me or instead of multiple objects one large object is created.

Library:

import java.util.Scanner;
import java.util.ArrayList;

public class Library {

    static ArrayList<Object> items = new ArrayList<Object>();
    static int menuOption;
    static Scanner scan = new Scanner(System.in);

    public static void main(String args[]) {
        String title, format;
        boolean right = false;

        do{
            displayMenu();
            if (menuOption == 1){
                System.out.println("Enter Title: ");
                title = scan.next();
                System.out.println("Enter format: ");
                format = scan.next();
                addNewItem(title, format);
            } else {System.out.println(items);

            }
        } while (!right);
    }

    static int displayMenu(){


        System.out.println("Menu: ");
        System.out.println("1. Add New Item");
        menuOption = scan.nextInt();

        return menuOption;
    }

    static void addNewItem(String title, String format){
        MediaItem b = new MediaItem();
        b.setTitle(title);
        b.setFormat(format);
        items.add(b);

    }
}

MediaItem:

public class MediaItem {

    String title;
    String format;

    MediaItem(){
        title = null;
        format = null
    }
    MediaItem(String title, String format){
        title = new String();
        format = new String();
    }
    public String getTitle() {
        return title;
    }
    public void setTitle(String title) {
        this.title = title;
    }
    public String getFormat() {
        return format;
    }
    public void setFormat(String format) {
        this.format = format;
    }
}
Jason C
  • 34,234
  • 12
  • 103
  • 151
YellowSoloCup
  • 91
  • 1
  • 4
  • 11
  • Which particular portion of your code are you struggling with? Please eliminate the irrelevant portions of code. – crush Mar 06 '14 at 04:36
  • Not clear what is wrong and what you need. – Sanjeev Mar 06 '14 at 04:39
  • 1
    Your code does not seem to match your subject title at all. – Scary Wombat Mar 06 '14 at 04:41
  • Are you looking to convert Arrays into ArrayLit? If yes, then take a look at (http://stackoverflow.com/questions/157944/how-to-create-arraylist-arraylistt-from-array-t?rq=1) – chauhraj Mar 06 '14 at 04:44
  • what are you looking for exactly ? – Mehdi Mar 06 '14 at 05:10
  • I apologize. I am trying to input multiple arrays and have the program read me back those arrays. I have been unable to obtain this result. I keep getting one big array instead of a bunch of individuals ones. – YellowSoloCup Mar 06 '14 at 06:03

2 Answers2

1

The program will run if you:

1 - Change the line

static ArrayList<Object> items = new ArrayList<Object>();

to

static ArrayList<MediaItem> items = new ArrayList<MediaItem>();

2 - Change the line

System.out.println( items );

to

for ( MediaItem mi : items )
{
    System.out.println( mi.getTitle() + ", " + mi.getFormat() );
}

3 - Insert a ";" at the end of the line

format = null

I did it here and it worked.

sflr
  • 2,177
  • 1
  • 25
  • 22
0

I either keep having the location where it is saved in memory returned to me

I am guessing you ran into this when you tried to either use System.out.println() to print a MediaItem, or you otherwise tried to automatically convert an object to a string. Whatever approach you took when you were seeing the memory addresses is probably the right way to do it, your problem was only in your displaying of the data.

Consider:

MediaItem item = ...;
System.out.println(item);

By default, Java doesn't know how to convert arbitrary objects to strings when you do stuff like that, and so it just spits out the class name and memory address. You either need to print the fields separately (e.g. Java knows how to display a String already), like:

System.out.println("Title: " + item.getTitle() + " Format: " + item.getFormat());

Or you can override toString() (declared in Object) to provide a custom string conversion:

class MediaItem {
   ...
   @Override public String toString () {
      return "Title: " + title + " Format: " + format;
   }
}

And then you can print it directly:

System.out.println(item);

It is the default base implementation of Object.toString() that produces those strings with the memory address in them.

Based on your description, I'm guessing you had a roughly working implementation but ran into this issue and ended up changing around (and breaking) a bunch of other unrelated things to try and fix it.

Jason C
  • 34,234
  • 12
  • 103
  • 151
  • Thank you very much for the help! yes I knew I was close to having it figured out and was wondering if it had something to do with converting. – YellowSoloCup Mar 06 '14 at 06:28
  • @YellowSoloCup By the way, if you're using Eclipse, you can right-click on your code, choose "Source", and then choose "Generate toString()" and Eclipse will generate a reasonable `toString()` function for you that includes the values of all the member fields. It's very convenient. – Jason C Mar 06 '14 at 06:33