13 Replies - 342 Views - Last Post: 26 November 2012 - 08:00 AM Rate Topic: -----

#1 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Accessing ArrayLists from seperate file

Posted 23 November 2012 - 10:54 PM

I'm trying to build a program for cataloging a small library for a local club I'm a part of (This is something I wish to do, I'm not being payed or commissioned to do it, it's more of a technical exercise for me). The design of the program is such that there is a few different '.java' files, and in one of them is the ArrayList in which everything is being read and written from/to.

My problem is that I'm not sure if you can access this ArrayList within a class which has been called from a separate '.java' file. And if it can be done, how would this be done.
The ArrayList is set as a public type for the purpose of access across classes (and hopefully files).

I can put up the code, or part of, if it would help.



Some help with this would be most appreciated.


~kosmro~

Is This A Good Question/Topic? 0
  • +

Replies To: Accessing ArrayLists from seperate file

#2 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Accessing ArrayLists from seperate file

Posted 23 November 2012 - 11:08 PM

Are they in the same package? You could always import the class and use the ArrayList that way. But that makes it a global list, which is frowned upon.
Was This Post Helpful? 0
  • +
  • -

#3 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Accessing ArrayLists from seperate file

Posted 23 November 2012 - 11:23 PM

They are in the same file/directory, if that's what you mean.

I'm not sure what a 'global list' is as such (my java knowledge is fairly limited still).
Was This Post Helpful? 0
  • +
  • -

#4 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Accessing ArrayLists from seperate file

Posted 23 November 2012 - 11:30 PM

A global item is an item shared among all classes/objects. The problem with a global is this.

Class A sets a value in global list to 5. Class B later sets that same value to 142. When class A goes back to look at the value, it's no longer the 5 it expected it to be see?
Was This Post Helpful? 0
  • +
  • -

#5 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 12:57 AM

I''l try to show what I'm trying to attempt.

This is the code for the 'Library.java' file:
/*
 * @file: Library.java
 * @author: Robert Kosmac
 * @version 22-Nov-2012
 * @description: This class implements the root menu and array.
 * It also allows adding to the database (array)
 */
import java.util.*;
import java.io.*;

public class Library{
    public ArrayList<LibItem> libarr = new ArrayList<LibItem>();
    public int counter = 0;
    public Scanner menu1 = new Scanner(System.in);
    public boolean flag = true;
    
    
    public Library(){
        
        readfile();
        runMenu();
    }//Library
    
    private void runMenu(){
        System.out.println("Please select the required task from below menu");
        while(flag){
            System.out.println("\n--------MAIN MENU--------\n" +
                               "1. Loans/Returns\n2. Search for media\n" +
                               "3. Admin Menu\n4. Quit");
            int menu1choice;
            menu1choice = menu1.nextInt();
            switch(menu1choice){
                case 1: //Loans/Returns
                {
                    Scanner find = new Scanner(System.in);
                    boolean contin;
                    System.out.println("Please enter the code of the book");
                    String cdsrch = find.next();
                    
                    //find item for loan or return
                    for(LibItem x: libarr){
                        if(x.getCatCode().equals(cdsrch)){
                            Scanner loanout = new Scanner(System.in);
                            System.out.println("x.toString");
                            
                            //loan or return check
                            if(x.getOnLoan() == false){
                                //Not on loan
                                
                                System.out.println("Do you wish to loan out " + x.getTitle() + "?");
                                
                                String continStri;
                                Scanner checker = new Scanner(System.in);
                                System.out.println("Press 'y' for Yes, or 'n' for No");
                                continStri = checker.next();
                                if(continStri == "y" || continStri =="Y"){
                                    contin = true;
                                }
                                else{contin = false;}
                                
                                if(contin == true){
                                    String name, dudate;
                                    System.out.println("Enter the Name and Call of person loaning item:");
                                    name = loanout.nextLine();
                                    x.setLoanedTo(name);
                                    System.out.println("Enter the due date");
                                    dudate = loanout.next();
                                    x.setDueDate(dudate);
                                    x.setOnLoan(true);
                                }
                                else{System.out.println("Item doesn't exist\n");}
                            }//if
                            else{
                                System.out.println("Would you like to return " + x.getTitle() + "?");
                                String continStri;
                                Scanner checker = new Scanner(System.in);
                                System.out.println("Press 'y' for Yes, or 'n' for No");
                                continStri = checker.next();
                                if(continStri == "y" || continStri =="Y"){
                                    contin = true;
                                }
                                else{contin = false;}
                                
                                if(contin == true){
                                    x.setLoanedTo(null);
                                    x.setOnLoan(false);
                                }
                            }//else
                        }//if
                    }//for
                }//case 1
                break;
                
                case 2: //Seach Menu
                    //SearchMenu sear = new SearchMenu();
                break;
                
                case 3: //Administration Menu
                    AdministrationMenu();
                break;
                
                case 4: //Exit
                    flag = false;
                break;
                
                default:
                    System.out.println("Bad selection. Must be from 1 -> 4");
                break;
            }//Switch
        }//while
    }//runMenu
    
    
    
    
    
    
    
    public void readfile(){
        try{
            Scanner readTextFile = new Scanner(new File("MRARC_Library.txt")).useDelimiter(":");
        
            while(readTextFile.hasNext()){
                String catCode = readTextFile.next();
                String loanedTo = readTextFile.next();
                String dueDate = readTextFile.next();
                String title = readTextFile.next();
                String author = readTextFile.next();
                String subject = readTextFile.next();
                String format = readTextFile.next();
                int year = readTextFile.nextInt();
                int edition = readTextFile.nextInt();
                boolean onLoan = readTextFile.nextBoolean();
                
                LibItem f = new LibItem(catCode, loanedTo, dueDate, title, author, subject, format, year, edition, onLoan);
                libarr.add(counter, f);
                readTextFile.nextLine();
                counter++;
            }
        }//try read
        catch(Exception e){
            System.out.println("Exception: " + e.getMessage());
        }//catch read
    }//readfile
    
    
     public void savefile(){
        try{
            PrintWriter writer = new PrintWriter("MRARC_Library.txt");
            for(int c = 0; c<counter; c++){
                writer.print(libarr.get(c).getCatCode() + ":" + libarr.get(c).getLoanedTo() + ":" + libarr.get(c).getDueDate() +
                ":" + libarr.get(c).getTitle() + ":" + libarr.get(c).getAuthor() + ":" +
                libarr.get(c).getSubject() + ":" + libarr.get(c).getFormat() + ":" + libarr.get(c).getYear() +
                ":" + libarr.get(c).getEdition() + ":" + libarr.get(c).getOnLoan() + ":\n");
            }
            writer.flush();
            writer.close();
        }//try save
        catch(Exception e){
            System.out.print("Exception: " + e.getMessage());
        }//catch save
    }//savefile
    
    
    
    
    
    
    public void AdministrationMenu(){
        //int addcount = counter;
        boolean adminflag = true;
        
        //implement the Administration Menu
        while(adminflag){
            Scanner adminmenuopt = new Scanner(System.in);
            //list menu options
            System.out.println("\n\n--------ADMINISTRATION MENU--------" +
            "\n\nPlease select one of the following");
            System.out.println("1. Add an item to the library\n2. Remove an item from the library" +
                               "\n3. Exit");
            int adminmenu = adminmenuopt.nextInt();
            //Begin Switch case menu
            switch(adminmenu){
                case 1: //adding to the library
                    Scanner adderf = new Scanner(System.in);
                    System.out.println("Please enter the Cataloging code of the item");
                    String catCode = adderf.nextLine();
                    String loanedTo = null;
                    String dueDate = null;
                    System.out.println("Title?");
                    String title = adderf.nextLine();
                    System.out.println("Author?");
                    String author = adderf.nextLine();
                    System.out.println("Topic or subject?");
                    String subject = adderf.nextLine();
                    System.out.println("Item format? (book, CD, etc)");
                    String format = adderf.nextLine();
                    System.out.println("Year of publish?");
                    int year = adderf.nextInt();
                    System.out.println("Publish edition or version?");
                    int edition = adderf.nextInt();
                    boolean onLoan = false;
                    
                    //create object and add to arraylist
                    LibItem f = new LibItem(catCode, loanedTo, dueDate, title, author, subject, format, year, edition, onLoan);
                    libarr.add(counter, f);
                    counter++;
                 break;
                 
                 case 2: //Remove an item from the array
                    System.out.println("Currently no code for this part");
                 break;
                 
                 case 3:
                    adminflag = false;
                 break;
                 
                 default:
                    System.out.println("Bad entry");
                 break;
                }//switch
            }//while
        }//AdministrationMenu
                    
                    

    
    
}//class



The ArrayList is located in this file, at line 12, and the 'SearchMenu' class is called at line 95 (I commented it out as the file is currently empty).

In the 'SearchMenu.java' file, I want to access the ArrayList from 'Library.java', so it can be searched for various entries.


At this stage, I have put the 'Administration' menu in a separate class in the 'Library.java' file, I would like to have this in a separate file too, it would need to be able to edit the ArrayList though, and I wondered if saving the file before the class is loaded, then load the saved file to a separate ArrayList to edit, then save on exit, and then the Library.java would reload the edited save to the ArrayList...but I think this may be rather inefficient and rather messy.
(The Administration menu is called in case 3, on line 99, and the 'AdministrationMenu()' class starts at line 169 to 223).


Does this help you help me at all? or just confuse the matter?
Was This Post Helpful? 0
  • +
  • -

#6 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 07:51 AM

First things first. You have more than one scanner with System.in parameters in your runMenu method. You can get rid of all but one scanner and use it everywhere.

I don't know what your Administration and SearchMenu classes look like so I can't comment on them. I don't think it's a good idea to save your library to a text file every time so you can share it between your library and SearchMenu. But I do think that if your searchMenu is only searching the library for some sort of media, then it should be a method in the library class no?
Was This Post Helpful? 0
  • +
  • -

#7 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 04:59 PM

I am aware of the multiple scanners, I usually do this to make sure everything is working properly first, then I start trimming what really isn't necessary (such as the multiple scanners). The 'Administration' menu is currently in the Library file (which I have posted just above), and with the search menu, I'm yet to write it (I wanted to work out how to access the ArrayList from another file first).

I wanted to put the 'Administration menu' and the 'Search Menu' in separate files, to keep the files a little neater...But it would seem that this may not be possible, or somewhat difficult at least.


If I am reading this right, it would be far easier to simply keep it all in the one file, simply as separate classes no?

~kosmro~
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Accessing ArrayLists from seperate file

Posted 24 November 2012 - 10:27 PM

You can't access ArrayList or actually any Java Object from a file
so your topic title just does not make sense
What are we supposed to understand from that topic title ?

I guess, correct me if I am wrong, you need to save and read back that ArrayList from file
Was This Post Helpful? 0
  • +
  • -

#9 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Accessing ArrayLists from seperate file

Posted 25 November 2012 - 09:24 PM

I shall try to show you the structure as best I can:

____________
LibItem.java
------------
Contains constructor,
set/get methods,
toString method.


^
|
|
|
|

____________
Library.java
------------
Contains ArrayList,
main menu (switch case)
read/write for text file
Will call/run AdminMenu.java


|
|
|
|
|
v

______________
AdminMenu.java
--------------
Contains Search methods for ArrayList
Editors for ArrayList (add/remove)






Does this at all help explain what I mean by from another file?

I had only just done work on inheritance and super/sub classes at Uni (in Programming 1), so I hadn't done anything quite like this, but I thought it would be a similar process to complete such a task. But by the sounds of it, I can't achieve such a feat.

Thanks all for your time and help/advice.

~kosmro~

This post has been edited by kosmro: 25 November 2012 - 09:31 PM

Was This Post Helpful? 0
  • +
  • -

#10 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Accessing ArrayLists from seperate file

Posted 25 November 2012 - 09:31 PM

Yes almots all Java classes, contained in a .java file contain
- a contructor
- getter(s)/setter(s)

sill does not explain your problem/question
Was This Post Helpful? 1
  • +
  • -

#11 kosmro  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 63
  • Joined: 07-March 12

Re: Accessing ArrayLists from seperate file

Posted 25 November 2012 - 09:46 PM

Scenario for problem:
While running the 'Library.java' file menu, I select the option in the menu for the Search Menu (to search for item in the ArrayList by title, author, year, etc). The SearchMenu.java file is called and run with the following line:
SearchMenu sear = new SearchMenu();

That bit is fine.

Problem:
The search's require access to the ArrayList, which is in 'Library.java'. The 'Search' methods are written similar to the following;
Scanner find = new Scanner(System.in);
boolean contin;
System.out.println("Please enter the code of the book");
String cdsrch = find.next();
                    
//find item for loan or return
for(LibItem x: libarr){
    if(x.getCatCode().equals(cdsrch)){
        System.out.println("x.toString");
    }
}







Question:
Can this be achieved? And if so, how?



~kosmro~
Was This Post Helpful? 0
  • +
  • -

#12 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Accessing ArrayLists from seperate file

Posted 25 November 2012 - 10:13 PM

I quit.
How can you expect to fix a problem you can't even explain ?

If your ArrayList is in an instance of a Library class who created it ? Who has access to it ?
Was This Post Helpful? 0
  • +
  • -

#13 nunc  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 131
  • Joined: 20-November 11

Re: Accessing ArrayLists from seperate file

Posted 26 November 2012 - 06:47 AM

If you are searching your Library ArrayList, then the searching should be a part of your Library class, not part of an "Admin Menu" class.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: Accessing ArrayLists from seperate file

Posted 26 November 2012 - 08:00 AM

Pass the ArrayList as parameter to the constructor of SearchMenu
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1