Results 1 to 15 of 15
  1. #1
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default Java Newbie - ArrayList Problem

    Hi there,
    I'm learning Java and I'm doing some project for university. I have a problem that I don't know how to solve it. I have a static ArrayList in some class that have an object with some properties. The user needs to insert some information that I need to compare if exists in that Object.property. I tried the compare method but I can't compare that 2 things. Can someone help me? Thanks in advance. Sorry about my english, it's not my native language.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    I can't compare that 2 things.
    Please post the code so we have sometime to talk about.

  3. #3
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    I don't think that could be more vague... can you post a simple example of code that shows what you're talking about?

  4. #4
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    Going to explain better.

    - in my class I have a ArrayList the receives Objects with these properties :

    licence, date, plate,hour

    - The user insert a licence and then I have a method called checkExists.


    - checkExist receives a licence

    - Then I'm trying to use a foreach to check all my ArrayList

    and I'm trying to compare if the Licence inserted exist in one of my ArrayList members.


    public void checkExist(String licence){

    this.licence=licence;


    for (Park Search : Container) {

    }
    if (Container.contains(licence)){

    System.out.println("Licence Exist");

    }
    else {

    System.out.println("No Licence");
    }

    }

    Can you guys understand now?

    Thanks for your help

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    You need to post the code for the class with the properties that you are having problems with.
    The code you posted is pseudo code, not compilable.
    You need to show the types for the variables: Park and Container

    Is Container the Java class? Or is it one of your classes?

    receives Objects
    You should use the name of the class not the word Objects. Object is a java class.

    Please post real Java code that shows your problem.

  6. #6
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    This is the Class that I'm working. Is this what you need?

    Thanks

    public class Park {

    public static HashSet<Park> Container = new HashSet<Park>();



    String plate;
    Integer licence;
    int active;
    Date entrancehour, leavehour,entrancedate,leavedate;


    public void checkExist(String licence){

    this.licence=licence;


    for (Park Search : Container) {

    }
    if (Container.contains(licence)){

    System.out.println("Licence Exist");

    }
    else {

    System.out.println("No Licence");
    }

    }


    some getter & setter here...

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Please edit your post and add code tags around your code so that we can read it. To do this add the tag [code] above your code and the tag [/code] below your code like so:

    Java Code:
    [code[i][/i]]
      // your code goes here  
    [/code[i][/i]]

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Container is a bad name for a variable. Container is a java class. Variables should start with lowercase letters.

    Can you explain what your problem is?
    Do you get errors? If so, please copy and paste them here.
    Do you have logic errors? If so, please explain what the code does incorrectly and what you want it to do.
    A program to compile and execute that shows the problem would be very useful.

    The HashSet contains Park objects. The contains method's argument(a String) is not a Park object. It should never return true.

    I do not know what your problem is yet. Please explain.
    Last edited by Norm; 06-05-2011 at 02:23 AM.

  9. #9
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    The problem is that I can't compare if my licence that user inputs exists inside my arraylist. The program works but I'm always getting the message "No Licence" even if I have inserted the same license.

  10. #10
    Toll's Avatar
    Toll is offline Senior Member
    Join Date
    May 2011
    Location
    Sweden
    Posts
    384
    Rep Power
    4

    Default

    I'll boil the code down a bit for you...

    Java Code:
    public static HashSet<Park> Container = new HashSet<Park>(); 
    Integer licence;
    public void checkExist(String licence){
    this.licence=licence;
    if (Container.contains(licence)){
    System.out.println("Licence Exist");
    }
    If I read the code correctly, you've got a HashSet that takes Park objects and an Integer called licence. In the checkExists, you take in a String called licence, set the Integer licence to the String licence and tries to get the String licence from the HashSet that contains Park. Does this code even compile?

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Are you reading what we've posted?

    Did you see this:
    The HashSet contains Park objects. The contains() method's argument(a String) is not a Park object.
    It should never return true because the HashSet does not contain Strings.

  12. #12
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    There was a mistake when I putted the code here. The method receives an Integer value and not String. Yes the code compile without any problem I just can't compare inside the if() statement. That's my only problem.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Please post the current code. Hard to talk about problems in code we can not see.

    Are you reading what we've posted?

    Did you see this:
    The HashSet contains Park objects. The contains() method's argument(a String) is not a Park object.
    It should never return true because the HashSet does not contain Strings.

  14. #14
    toxicrainpx is offline Member
    Join Date
    Jun 2011
    Posts
    6
    Rep Power
    0

    Default

    OK, I have re-writed all my code the a new Class. The user insert the number and the method needs to find if that number it's inside the HashMap (I decided that I should use a HashMap because "numero" can't be duplicated). I'm going to paste my code :

    Java Code:
    import java.util.Collection;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Iterator;
    
    public class Entrada {
    	
    	public static HashMap<Integer,Entrada> LugaresEstacionamento = new HashMap<Integer,Entrada>();
    	
    	Integer numero;
    	int activo;
    	String matricula;
    	Date dadosentrada;
    	
    	public Entrada(){
    		
    		this.numero =0;
    		this.matricula="";
    		this.activo=0;
    	
    	}
    	
    	
    	
    	public Entrada verifyPark(Integer numero){
    		
    		//receives the user input numero
    
    
    
    		Entrada  Verify = new Entrada();
    		
    		Collection<Entrada> ParkedVeicules = Verify.LugaresEstacionamento.values();
    		Iterator IT = ParkedVeicules.iterator();
    
    		while (IT.hasNext()){
    			
    			//verify if the LugaresEstacionamento.numero have the inserted numero
    
    			if (ParkedVeicules.contains(numero)) {
    
    				System.out.println("There's a Car Inside the Park");
    				
    
    			}
    			else
    			{
    
    				System.out.println("There's no Car Inside the Park");
    				
    			}
    
    
    		}
    		
    		return null;
    
    	}
    
    	
    	
    	
    	
    	public Entrada enterPark() {
    		
    		//saves the inputed values on the HashMap
    		
    		return LugaresEstacionamento.put(numero,this);
    	}
    
    	
    	
    	
    
    }
    Can you help me? I don't know what to do anymore.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Have you read the API doc for the contains method? Please do it.

    Does the ParkedVehicles contain Integer objects?
    It looks like ParkedVehicles contains Entrada objects.
    The contains() method does NOT (normally) look inside of the objects contained in the collection. It only tests if there is a reference to an object in the collection that is the same exact object as the reference argument in the contains method.

    Here is a test program that shows what I'm talking about:
    Java Code:
          HashSet<AClass> someObj = new HashSet<AClass>();
          someObj.add(new AClass("FIrst"));
          someObj.add(new AClass("second"));
          AClass aSec = new AClass("second");  // new but same value
          someObj.add(aSec);  // THE EXACT SAME OBJECT to Find !!!  Comment this out and try it
          if (someObj.contains(aSec))
            System.out.println("found second");
          else
            System.out.println("did not find second");
    Last edited by Norm; 06-05-2011 at 06:16 PM.

Similar Threads

  1. Newbie problem
    By hyookai in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 02:30 PM
  2. [newbie] bracketing problem
    By jon80 in forum New To Java
    Replies: 15
    Last Post: 05-30-2009, 03:28 AM
  3. [newbie] getting class members from Arraylist
    By jon80 in forum New To Java
    Replies: 16
    Last Post: 05-15-2009, 07:45 AM
  4. Newbie help with ArrayList!
    By oldmanhouse in forum New To Java
    Replies: 2
    Last Post: 11-05-2008, 11:46 PM
  5. Replies: 2
    Last Post: 04-21-2008, 11:43 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •