Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 22 of 22

Thread: Java help, passing/filtering a array

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java help, passing/filtering a array

    Hello,
    I wont post the whole program because it is a lot of classes, interfaces and abstract classes.


    I am trying to filter a array by simply getting a value which is Z. The String representation of the whole application is

    1,1,1:3,4,2:RoomA
    x,y,z h,w,l name

    The z parameter is suppose to filter through and only allow any Array above z > 1. It then returns the filter array.


    I have done something similer but returns the full arraylist by coverting a array.




     
    public static ArrayList<Room> rooms = new ArrayList<Room>();
     
    public class Storey implements Buildable{
     
    public static  Room[] getAllRooms()
    	{
     
     
    		 Room[] array = new Room[rooms.size()];
    		array = rooms.toArray(array);
     
     
    		return  array = rooms.toArray(array);
    }		
    	}


    The below code needs to filter it by making anything above Z then returning it.

     
    public class engine implements model {
     
    public Room[] getStoreyRooms(int storey) {
     
    		//storey = RoomReference.getZ();
     
     
     
    		 Room[] array = new Room[Storey.rooms.size()];
     
    		// array = Storey.rooms.toArray(array);
     
     
    		 array.equals(storey!=1);
     
    		 {
     
    			// if storey.getLocation()
     
              return Storey.rooms.toArray(array);
    		 }
     
     
    	}

    This is the class it gets it from.

    public class RoomReference {
    private int x;
    private int y;
    private int z;
    private AbstractRoom abstractRoom ;
     
     
     
    	public RoomReference(int x, int y, int z) {
     
     
    	this.abstractRoom = abstractRoom;
     
    	this.x = x;
    	this.y = y;
    	this.z = z;
    }
    	public int getX()
    	{
    		return x;
    	}
    	public int getY()
    	{
    		return y;
    	}
    	public int getZ()
    	{
    		return z;
    	}
     
    	public String toString()
    	{
     
    		String s = "";
     
    		s += x;
    		s += ",";
    		s += y;
    		s += ",";
    		s += z;
    		s += ":";
    		return s;
     
    	}
     
     
     
    }

    I hope this is enough , but if you need me to post more please dont hesitate to ask.


  2. #2

    Default Re: Java help, passing/filtering a array

    When you say filter, what is it you really want to do with the array?
    Do you specifically want to filter an ArrayList?

    When your filter matches, do you want to remove the matched items or keep only the matched items?

    It is generally a poor idea to name variables single letters unless they are for counting in loops. Maybe you should rename your variables to something that has context with the situation you are in.

    Please give some more details.
    Kenneth Walter
    Software Developer
    http://kennywalter.com

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    I want to get any array to match with the data z > 1, So it will return from the method.

    Lets just say we have data populated in the arraylist.
    1,1,1:3,4,2:RoomA
    1,2,1:5,4,3:RoomB
    1,1,2:2,2,2:RoomC

    Ok , we have x,y,z ,height,width,length , name


    Checking rooms with getStoreyRooms()
    1,1,2:2,2,2:RoomC

    As you see only one Room in the arraylist shows because the z value is higher then 1.


    It needs to be implement in this method with the following parameters.
    and yes it needs to be coverted to array from a arraylist as the last example.

    It is in the model interface,which the content of the method must be on the engine, as shown below.
    public class engine implements model {
     
    public Room[] getStoreyRooms(int storey) {
     
    }

    Think of x,y,z as coordiates , if it is 2,2,2
    [][][]
    [][X][]
    [][][]

    z as mutiple level building
    I simply want to keep the matches, and get rid of the rest. anything that matches higher then z>1

    does this answer your question?,if more detail is require please reply

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    Lets just say we have data populated in the arraylist.
    1,1,1:3,4,2:RoomA
    1,2,1:5,4,3:RoomB
    1,1,2:2,2,2:RoomC
    Does your ArrayList in this example contain three elements? What data type are the elements?
    Are they Strings? Like this: "1,1,2:2,2,2:RoomC"
    Or is 1,1,2:2,2,2:RoomC a representation of the contents of a Room object?

    For the example you posted, would getStoryRooms() return an array with a single element?
    A Room with the value: 1,1,2:2,2,2:RoomC

  5. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Java help, passing/filtering a array

    You should have a Room class if you don't already.
    for each room {
        if z value is greater than 1 {
            add room to temp list;
        }
    }
    return temp list;
    Improving the world one idiot at a time!

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    yes, i have a room class and yes it is a representation of Room ,


    This is the AbstractRoom class it implements Room.

    Under AbstractRoom are the classes that contain the values of each element used for the arraylist, such as the class RoomReference with x,y,z. class Rectangle with width height length, and this name on this class private String name is the roomsname. It connects with different classes then combines together, the linking of each class is correct since the other method getAllRooms() is successful, now i am trying to filter using the (z>1). I can attach the uml diagram if needed?

    igorne the other arraylists.

    public interface Room extends Buildable{
     
    	public void addExit();
    	public void addItem();
    	public String getExit();
    	public ExitPoint getExitList();
    	public String getItem();
    	public String getItemList();
    	public RoomReference getLocation();
        public String getName();
    	public void removeExit();
    	public void removeItem();
    	public String toString();
    	public Object getExit(String string);
     
     
     
     
     
    }

    public abstract class AbstractRoom implements Room{
     
     
    	private  String name;
    	private RoomReference location;
    	private ArrayList<ExitPoint> exits = new ArrayList<ExitPoint>();
    	private ArrayList<Item> items = new ArrayList<Item>();
     
    	public AbstractRoom(RoomReference location, String name)
    	{
     
    		this.location = location;
    		this.name = name;
    		this.items = new ArrayList<Item>();
     
    	}
     
    	public void addExit(ExitPoint v)
    	{
    		exits.add(v);
     
    	}
     
    	public void addItem(Item v)
    	{
    		items.add(v);
     
    	}
    	public void clearArea()
    	{
    		exits.clear();
    		items.clear();
    	}
    	public ExitPoint getExit(ExitPoint v)
    	{
    		return v;
     
    	}
    	public ExitPoint getExitList()
    	{
    		//array list
    		return null;
     
    	}
    	public Item getItem(Item i)
    	{
    		return i;
     
    	}
    	public String getItemList()
    	{
    		return null;
     
    	}
    	public RoomReference getLocation()
    	{
     
     
    		return location;
     
    	}
    	public String getName()
    	{
    		return name;
    	}
    	public void removeExit()
    	{
     
    	}
    	public void removeItem()
    	{
     
     
    	}
     
     
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    Where is the definition of z in the definition of those two classes you just posted?
    If z is not defined in the class, how can you use z's value to "filter" an instance of the class?

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    it runs though the RoomReference class.



    so RoomReference ----> Abstractroom <---- rectangleroom

    Room implements Abstractroom


    I have posted the UML, to hopefully show we are on the same page. attribute not included.

    I havnt uploaded the engine class or the model.


    IMG.jpg


    How does the Z value connect?

    it connects by
    public AbstractRoom(RoomReference location, String name)
    	{
     
    		this.location = location;
    		this.name = name;
    		this.items = new ArrayList<Item>();
     
    	}
    RoomReference location, has the getZ() connect to it using the contractor.


    As you see it all connects, I have tested the arraylist and it does successfully get the Z value using the toString() on each class. But once it is on its String representation, I cant slice it out to test the specify Z value. It shows the whole arraylist entire which i cant do (z>1);

    a Element
    1,1,2:2,2,2:RoomC

    1,1,2
    RoomReference

    :2,2,2
    Rectangleroom
    :RoomC
    Name

    which the classes are called by the engine class, then to the facade class.
    Last edited by jack55655; September 2nd, 2011 at 01:06 AM.

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    But once it is on its String representation, I cant slice it out to test the specify Z value
    Why are you trying to work with the String representation of the class? You should be working with the Room objects.

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Location
    1020 19th St NW, Washington, DC 20036
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    There is no direct way to remove elements from an array, because its size is fixed. Whatever you do, you need to allocate a new array somehow.

  11. #11
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    I have created a new array on the method , as shown. String representation is needed to connect to other classes like roomReference by using the toString method.



    Below is code I done. r is for RoomReference I created a instance of the object in the contractor, i can only do this without making it static.
     
    	public Room[] getStoreyRooms(int storey) {
     
    		storey = r.getZ();
     
     
     
     
    		 Room[] a = new Room[s.rooms.size()];
     
    		 System.out.println(a);	
    	       if(1 > storey)
    	       {
    	    	   return a;
    	       }				 		
    		return null;
    	}

    This line gives me a nullpointerexception
    storey = r.getZ();
    I would assume it would be able to get the Z value from the RoomReference class? and not give me a null.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    This line gives me a nullpointerexception
    storey = r.getZ();
    Does the r variable have a valid value or is its value null?
    Where do you give the r variable the address of a Room object?

  13. #13
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    r is for RoomReference it reads the methond on the class that has getZ();

    public engine(String name,int budget)
    	{
    		s = new Storey();
    		o = new Owner(name, budget, budget);
    		h = new House();
    		RoomReference r = new RoomReference(1, 1, 1);
     
    	this.name = name;
    	this.budget = budget;
    	}

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    The r variable in the engine constructor is local to the constructor. You need to move its definition outside of the constructor.

  15. #15
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    so to a different class?

    I have notice it reads the local constructor but i dont know how i would implement it to read the contractor with the data it needs, i believe the method is corrected now. i need to find a way to access the data instead of the local data in the class. just give me a bit to try something out, please dont lock this thread.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

            RoomReference r = null;  // define the variable here
     
     
            public engine(String name,int budget)
    	{
    		s = new Storey();
    		o = new Owner(name, budget, budget);
    		h = new House();
    		r = new RoomReference(1, 1, 1);  // give it a value here
     
    	this.name = name;
    	this.budget = budget;
    	}

  17. #17
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    I have tried the above no sucess, it still gives me a null exception.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    it still gives me a null exception.
    Please copy and paste the FULL text of the error message.

    There are many ways for r to be null.

    Are you sure that this statement:
    r = new RoomReference(1, 1, 1)
    is executed BEFORE you try to use the value of r?

    Is there more than one r variable? And you are using one that does not have a value.

  19. #19
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    	public Room[] getStoreyRooms(int storey) {	
     
     
    		 storey = r.getZ();	
    		 Room[] a = new Room[Storey.rooms.size()];
     
     
    		 System.out.println(Storey.rooms);
     
     
    	       if(1 < storey)
    	       {
    	    	   return a;
    	       }	
    		return null;
    	}



     RoomReference r = null;
     
    	public HomeCADengine(String name,int budget)
    	{
    		s = new Storey();
    		o = new Owner(name, budget, budget);
    		h = new House();
    		RoomReference r = new RoomReference(1, 1, 1);
     
    	this.name = name;
    	this.budget = budget;
    	}





    I have tried the above no sucess, it still gives me a null exception, I am using the r value.


    Error as shown below:

    at model.facade.engine.getStoreyRooms(engine.java:62)
    at test.Test.testAddRoom(Test.java:141)
    at test.Test.main(Test.java:1157)

    The Test.java file is definately correctly.
    Last edited by jack55655; September 3rd, 2011 at 11:16 AM.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    Where do you give r a value? Your code does not show where the r variable that is used in getStoreyRooms() is gettting a value.

    You have the same problem in this code that you had before. You have defined a local variable with the same name as the class variable. That definition hides the class variable and does not change its value:
    RoomReference r = new RoomReference(1, 1, 1);

    Remove the RoomReference and leave it like this:
    r = new RoomReference(1, 1, 1);

  21. #21
    Junior Member
    Join Date
    Sep 2011
    Posts
    10
    My Mood
    Stressed
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java help, passing/filtering a array

    ok the new output is shown, as you see it fails to filter by only showing 1,1,2:2,2,2:RoomC::

     
     
    Checking rooms with getStoreyRooms()...[1,1,1:3,4,2:RoomA::, 1,2,1:5,4,3:RoomB::, 1,1,2:2,2,2:RoomC::]
    Parsing error: could not check room list successfully!



    This is the line it gives bad output, on the test.java
    String[] expected = { "1,1,1:3,4,2:RoomA::", "1,2,1:5,4,3:RoomB::",
    					"1,1,2:2,2,2:RoomC::" };
    			String[] expected1 = { "1,1,2:2,2,2:RoomC::" };
     
     
    if (failureCheck(checkForRooms(model.getStoreyRooms(2), expected1),
    						true))
    					return false;
    It shows the data on the arraylist but fails to filter it. the above (2) is expected to be the output. but it doesnt it shows all 3 arraylist entires.
    Last edited by jack55655; September 3rd, 2011 at 11:41 AM.

  22. #22
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Java help, passing/filtering a array

    What do you expect anyone to recommend from this little post?
    Is this what you are trying to do>
    You have an array list containing Room objects.
    You have a loop that goes through the objects in the array list and selects some based on the object's z value.
    The selected objects are to be removed from the original arraylist and added to a new array list.
    You then display the contents of the new arraylist.

    The problem is that an incorrect object is selected and moved to the new arraylist.

    The solution is to check the logic for making the selection. Why does it select an object with the incorrect z value?

Similar Threads

  1. passing a string and int array into a static method
    By u-will-neva-no in forum Java Theory & Questions
    Replies: 2
    Last Post: May 8th, 2011, 05:35 PM
  2. Objects passing to JSP
    By ober0330 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 28th, 2011, 03:19 PM
  3. HELP. Passing Array to class.
    By videodrome in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 13th, 2010, 04:35 AM
  4. Passing a value to another class
    By Semple in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 4th, 2010, 05:49 PM
  5. Passing in more than one flag
    By anon181 in forum Loops & Control Statements
    Replies: 3
    Last Post: February 2nd, 2010, 06:37 AM