1

I am running into some issues with my Java program. We have to create a library, which contains the title(halo, lotr) , the format (xbox, dvd etc), the date loaned (if it is ever loaned), and the person it is loaned to (if it is ever loaned).

I am not complete with my code, however I am testing it out as I go along instead of just compiling the entire finished code after 5 hours of coding. I am running into a problem. Whenever I set a public string variable to a value, it saves in the method I declared it in, but it will display "null" when system.out.print'd in other methods.

heres my code. First class is Library.

package p1;
import java.util.Scanner;
public class Library {
// \/ FIELDS
private String[] mediaItemTitle = new String[100];
public String[] mediaItemFormat = new String[100];
public String[] mediaItemLoanedTo = new String[100];
public String[] mediaItemOnLoan = new String[100];
public String[] mediaItemDateLoaned = new String[100];
public String today = "3/9/2015";
public int numberOfItems;   
// /\ FIELDS

// \/ METHODS
public static void main(String[] brad){

    Scanner input = new Scanner(System.in);

    MediaItem main;
    main = new MediaItem();
    String title;
    String format;
    String date;
    String name;

    for ( int i = 0; i != 5; ){
    i = displayMenu();
    if (i == 1){
        System.out.println("What is the title? ");
        title = input.nextLine();
        System.out.println("What is the format? ");
        format = input.nextLine();
        main.MediaItem(title,format);

    }else if (i == 2){
        System.out.println("Which Item (Enter the title? ");
        title = input.nextLine();
        System.out.println("Who are you loaning it to? ");
        name = input.nextLine();
        System.out.println("When did you loan it to them? ");
        date = input.nextLine();

    }else if (i == 3){
        main.MediaItem();
    }else if (i == 4){
        System.out.print("Which item? (enter the title) ");
        title = input.nextLine();
        main.markReturned(title);
    }else if (i == 5){ // DONE
        System.out.print("Goodbye!");
        break;
    }

    }

}
public static int displayMenu(){ // DONE
    Scanner input = new Scanner(System.in);

    int choice = 0;
    System.out.println("1. Add new item");
    System.out.println("2. Mark an item as on loan");
    System.out.println("3. List all items");
    System.out.println("4. Mark an item as returned");
    System.out.println("5. Quit");
    choice = input.nextInt();


    return choice;
}
public void addNewItem(String title, String format){
    this.mediaItemTitle[numberOfItems] = title;
    this.mediaItemFormat[numberOfItems] = format;
    System.out.print("TEST: " + mediaItemTitle[numberOfItems]);

}
public void incrementNumberOfItems(){
    numberOfItems++;
}
public void listAllItems(){
for (int i = 0; i < numberOfItems; i++){
System.out.print(mediaItemTitle[i])
}

} Here is the second part of code, my second class MediaItem package p1;

public class MediaItem {

// \/ METHODS
public void MediaItem(){
    Library list;
    list = new Library();
    list.listAllItems();

}
public void MediaItem(String title, String format){
    Library call;
    call = new Library();
    call.addNewItem(title, format);
    call.incrementNumberOfItems();

}



// /\ METHODS

}

This is driving me insane. I would love to just have me public variables save their value between methods but its not happening. the console (when 3 is chosen from displayMenu)

0 null

which means numberOfItems and mediaItemTitle[i] are read to be 0, and null. Which I dont understand, because I declared them earlier in the program!!!

I dont understand what Im doing wrong. please help me! Thank you!!

Brad
  • 13
  • 4
  • Comment: I set mediaItemTitle to private to see if it would have any effect. It didnt. – Brad Mar 09 '15 at 00:28

2 Answers2

1

Your main mistake is that you are creating a new instance of Library inside your MediaItem method. That Library object will only live in the scope of MediaItem method. Plus Library is your main static class.

Your design is all wrong in my opinion. It looks like you are learning you way to Java or OOP, which is perfectly fine to have these mistakes.

Separate your data from your main class, create new classes just for your data. Have a look at java POJO (Plain Old Java Objects), like here

For example:

String title;
String format;
String date;
String name;

Should be in a new object, a POJO. Something like:

public class MyDataPOJO {

   private String title;
   private String format;
   private String date;
   private String name;

   public MyDataPOJO(String title, String format, String date, String name) {
      this.title = title;
      this.format = format;
      this.date = date;
      this.name = name;
   }

   public String getTitle() {return title;}
   public String getFormat() {return formate;}
   // And the rest of the getter methods for date and name
}

In you Library class you may only need to hold your logic. But even that can be re-factored to another class.

On a side note, please check the java naming convention. Here is a guideline: link. In other words, start you methods name with lower case.

Example, your public void MediaItem(){/** something*/} should be public void mediaItem(){/** something*/ }

dazito
  • 7,740
  • 15
  • 75
  • 117
  • Thank you so much! And yes, I am just starting out in Java. I will try and follow code conventions to a more precise and exact standard. – Brad Mar 10 '15 at 13:58
0

Follow the answer above and treat this as a comment, since the persons answer is correct and my statement isn't regarding your primary problem.

In your for-loop, I think you should add another else if statement. If the user enters a number that is not 1-5, they should receive an error. So maybe something like

else if (i < 1 || i > 5)
  System.out.println("Error: Enter a choice 1-5\n");

Also, I think you may have forgotten a } to end your listAllItems() method.

But as I was saying, the answer to your real problem has already been handled, so give them the check mark. This is just a minor UI error I noticed.