Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

Java - passing an object array to a method?

  • 18-10-2007 10:15am
    #1
    Closed Accounts Posts: 20,759 ✭✭✭✭


    I have a class, one of the methods creates an array of objects. The size of the array is specified by another method. I might be going about this arseways, but this is the only way I can see. Anywho..

    pAdd method.
    public static void pAdd(int cellCount){
    	Prisoner[] p = new Prisoner[cellCount];
    	for(int i = 0; i < p.length; i++){
    		System.out.print("Inmate's forename: " );
    		p[i].firstName = Keyboard.readString();
    		System.out.print("Inmate's surname: " );
    		p[i].surName = Keyboard.readString();
    		System.out.print("Inmate's age: " );
    		p[i].age = Keyboard.readInt();
    		System.out.print("Inmate's forename: " );
    		p[i].sentence = Keyboard.readInt();
    		// automatically assign cell id.
    		p[i].cellNum = i;
    	}
    }
    

    Note (cellCount is being passed to the array to count the number of cells).

    So, I'm trying to pass the full p[] array to another method, but it's not passing.

    pDetails method
    public static void pDetails(p[]){
    	System.out.print("Enter prisoner ID: ");
    	int i = Keyboard.readInt();
    	System.out.println("Prisoner details.");
    	System.out.println("Cell ID: " + p[i].cellNum);
    	System.out.println("Cell ID: " + p[i].firstName);
    	System.out.println("Cell ID: " + p[i].surName);
    	System.out.println("Cell ID: " + p[i].age);
    	System.out.println("Cell ID: " + p[i].sentence);
    }
    

    The output error is: G:\Year 2\Server Scripting\Assignment 1\menuOption.java:59: <identifier> expected
    public static void pDetails(p[]){


    Here is my prisoner class just incase.

    Prisoner class.
    public class Prisoner{
    
    // variables
    int cellNum;
    String firstName;
    String surName;
    int age;
    int sentence;
    
    // constructor
    Prisoner(int cellNum, String firstName, String surName, int age, int sentence){
    	this.cellNum = cellNum;
    	this.firstName = firstName;
    	this.surName = surName;
    	this.age = age;
    	this.sentence = sentence;
    }
    
    
    }
    

    Ideally, I just want to pass the array to printing method just so I can print out a select prisoner details.

    Any help would be great.

    John.


Comments

  • Registered Users, Registered Users 2 Posts: 594 ✭✭✭eden_my_ass


    Didn't read through the rest of your code but it seems your immediate problem is that you haven't declared that parameter right....

    public static void pDetails(p[]){

    should be...

    public static void pDetails(Prisoner[] p){

    I think :)


  • Registered Users, Registered Users 2 Posts: 413 ✭✭ianhobo


    yep, i agree
    You didn't specify any type for the array that your passing in.
    So the comipler doesn't know if its an array of chars, ints, type object, elephants, or an array of objects of type prisoner ;)


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Ah yes! Thanks, you're correct.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Eek, now I'm getting an error with my switch menu when I call two methods (menuOption.pAdd() and menuOption.pDetails())

    Error message:
    pAdd(Prisoner[]) in menuOption cannot be applied to ()
    menuOption.pAdd();


    pDetails(Prisoner[]) in menuOption cannot be applied to ()
    menuOption.pDetails();


    Sorry for all the questions, we covered objects in 40 minutes last year so I'm not totally up on all the rules - I'm the only one in class who even has an idea where to begin on this program, it's far beyond the scope of what we covered last year :(


    Switch menu code
    public static void showSwitches(){
    	int i = 0;
    	i = Keyboard.readInt();
    
    
    	/*
    	user menu methods
    	see menuOption.java to see past method abstraction
    	*/
    
    	switch(i){
    
    		case 1:
    			menuOption.pAdd();
    		break;
    
    		case 2:
    			menuOption.listOldest();
    		break;
    
    		case 3:
    			menuOption.listYoungest();
    		break;
    
    		case 4:
    			menuOption.listSentence();
    		break;
    
    		case 5:
    			menuOption.listAverage();
    		break;
    
    		case 6:
    			menuOption.pDetails();
    		break;
    
    		default:
    			System.out.println("Invalid option selected.");
    		break;
    		}
    	}
    


    Other
    public class menuOption{
    
    Prisoner[] p; // create prisoner object
    
    
    // list youngest prisoner.
    public static void listYoungest(){
    		System.out.println("Youngest prisoner is: ");
    	}
    
    // construct prison size
    public static void constructCells(Prisoner[] p){
    		int cellCount;
    		System.out.print("How many cells do you require: ");
    		cellCount = Keyboard.readInt();
    		System.out.println(cellCount + " cells constructed.");
    		p = new Prisoner[cellCount];
    	}
    
    
    // list oldest prisoner
    public static void listOldest(){
    		System.out.println("Oldest prisoner is: ");
    	}
    
    
    // list longest prison sentence
    public static void listSentence(){
    		System.out.println("Longest sentence is: ");
    	}
    
    
    // list average prisoner age
    public static void listAverage(){
    		System.out.println("Average age is: ");
    	}
    
    
    // add a prisoner to the database
    public static void pAdd(Prisoner[] p){
    	for(int i = 0; i < p.length; i++){
    		System.out.print("Inmate's forename: " );
    		p[i].firstName = Keyboard.readString();
    		System.out.print("Inmate's surname: " );
    		p[i].surName = Keyboard.readString();
    		System.out.print("Inmate's age: " );
    		p[i].age = Keyboard.readInt();
    		System.out.print("Inmate's forename: " );
    		p[i].sentence = Keyboard.readInt();
    		// automatically assign cell id.
    		p[i].cellNum = i;
    	}
    }
    
    // view prisoner's details
    public static void pDetails(Prisoner[] p){
    	System.out.print("Enter prisoner ID: ");
    	int i = Keyboard.readInt();
    	System.out.println("Prisoner details.");
    	System.out.println("Cell ID: " + p[i].cellNum);
    	System.out.println("Cell ID: " + p[i].firstName);
    	System.out.println("Cell ID: " + p[i].surName);
    	System.out.println("Cell ID: " + p[i].age);
    	System.out.println("Cell ID: " + p[i].sentence);
    }
    
    }
    


  • Registered Users, Registered Users 2 Posts: 6,240 ✭✭✭hussey


    you are trying
    menuOption.pAdd();

    and you defined the method as
    public static void pAdd(Prisoner[] p){

    so you are supposed to pass in an object - but you are not


  • Advertisement
  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    I figured as much - but I have no need to pass it an object when I call the method. I have specified it in the interface of those particular methods because I created the array outside of the method - so I am eseentially bringing the array into the method so I can manipulate it. Have I gone about this all wrong? I won't be able to access the array otherwise will I?


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    I looked at the first bit of code only. Not sure why you are doing it that way. You would be better to create a bean and access the data that way.


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Hobbes wrote: »
    I looked at the first bit of code only. Not sure why you are doing it that way. You would be better to create a bean and access the data that way.

    It's the only way I know how - we really only covered basic java last year, I had to read up on objects and that myself. But I haven't covered beans. Could you be more specific about which part of the code I am doing awkwardly?


  • Registered Users, Registered Users 2 Posts: 5,401 ✭✭✭DublinDilbert


    dlofnep wrote: »
    It's the only way I know how - we really only covered basic java last year, I had to read up on objects and that myself. But I haven't covered beans. Could you be more specific about which part of the code I am doing awkwardly?

    Your class "Prisoner" is effectively like an Identity bean...

    You could also look at using a Vector to hold the objects of type prisoner... a vector is like an array, just a bit more flexible, you can add and remove objects from it quite easily... Also you can pass the vector between methods, or even over an object stream between 2 computers on a network...


  • Registered Users, Registered Users 2 Posts: 21,264 ✭✭✭✭Hobbes


    Basically your trying to store data into an array that would be better suited in a bean.

    So in the case of the original example you would create a bean as follows.
    class InmateBean { 
      private String firstName; 
      private String surName;
      private int age;
      private int cellNumber;
    
      public void setFirstName(String text) { 
        firstName = text;
      }
     
      public String getFirstName() { 
        return firstName;
      }
    
      // Do the same for the other fields
    }
    

    After that you just create an inmateBean.
    InmateBean prisoner = new InmateBean(); 
    
    prisoner.setFirstName("dlofnep");
    prisoner.setCell(12);
    
    System.out.println("Cell Number: " + prisoner.getCell());
    

    This way all your related data is contained within an object and can't be manipulated without requesting the data from the object. So for example you could put checks in on setCell so that they can't put a value >20. Data validation goes into the bean.

    Looks like you were close though with Prisoner.

    After that sending in the value to another function.

    public static void pDetails(Prisoner[] p){


  • Advertisement
  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    I briefly had a look at vectors about two weeks ago. I was going to construct it using vectors, but I figured - why not use an object array and I hadn't enough knowledge about vectors to do it..

    So is the problem I have unavoidable with no solution using my current practices? Can I not call that method without supplying a parameter? If I could access the object without specifying it when creating the method, then it wouldn't ask me to supply a parameter when I call it?


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    Ok thanks Hobbes. I'll give it a crack later.


  • Registered Users, Registered Users 2 Posts: 11,989 ✭✭✭✭Giblet


    Use an parameterised arraylist.

    List<Prisoner> p = new ArrayList<Prisoner>()

    Then when you want to add a prisoner go
    p.add(prisonerobject).

    Then you can call it's details up with p.get(i).WhateverPrisonerMethod

    Use a for each loop too.

    for(Prisoner myPrisoner: p)
    {
    myPrisoner.toString();
    }


  • Closed Accounts Posts: 20,759 ✭✭✭✭dlofnep


    I'll have a look, thanks.


Advertisement