Results 1 to 7 of 7
  1. #1
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default How to read a string in a ArrayList?

    Hello all!

    I've a arraylist, contains a String (kaartNummer), and two integers. Now I want to give a String (vertrekNummer), and I want to search in the arraylist if the string 'vertrekNummer' is the same as 'kaartNummer'. How to do so? Currently code:

    Java Code:
    	public void checkIn (String kaartNummer, int aankomstUur, int aankomstMinuten)
    	{
    		bezoeker1 = new MuseumBezoeker(kaartNummer, aankomstUur, aankomstMinuten);
    		bezoekerArray.add(bezoeker1);
    	}
    Java Code:
    	public void checkOut (String vertrekNummer, int vertrektUur, int vertrekMinuten)
    	{
    		for (int positie = 0; positie < bezoekerArray.size(); positie++)
    		{
    			if ( vertrekNummer.equals( kaartNummer ) ) // error of couse, but how to say 'if it equals to the 'kaartNummer' in the array?
    				{
    				    System.out.println( "gelijk"  + bezoekerArray.get(positie));
    				}
    			else
    				System.out.println( "ongelijk ");						
    		}		
    	}

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,932
    Rep Power
    8

    Default

    You already know how to iterate over the ArrayList. You already know how to get items from the ArrayList. You already know how to use the equals() method. What's your question?

  3. #3
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Hello all!

    I've a arraylist, contains a String (kaartNummer), and two integers. Now I want to give a String (vertrekNummer), and I want to search in the arraylist if the string 'vertrekNummer' is the same as 'kaartNummer'. How to do so?
    If your List contains Strings as well as ints (Integers actually) it is generally considerd AVBT (A Very Bad Thing (tm)). Do the String and the two ints make up another entity? If so you should define a separate class for it and store an object of that class in your List. Storing Strings and ints in a single List is so 1960s Fortran ...

    kind regardfs,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Eeh.. well, actually a 'bezoeker' (a object) is in the array. A 'bezoeker' is made of a cardname (kaartNummer, the String), and a time (a int for hours, and a int for minutes).

    Now I simply have to get the cardname (kaartNummer), out of the string from every position, and compare it to the last entered value in a textfield...

    So well, yea the string is in the same array as the two ints, but that is because it is together one object...

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    I think you mean to do this:
    Java Code:
    	public void checkOut (String vertrekNummer, int vertrektUur, int vertrekMinuten)
    	{
    		for (int positie = 0; positie < bezoekerArray.size(); positie++)
    		{
    			if ( vertrekNummer.equals( [COLOR="Blue"]bezoekerArray.get(positie).kaartNummer[/COLOR] ) ) // error of couse, but how to say 'if it equals to the 'kaartNummer' in the array?
    	// or?		if ( vertrekNummer.equals( [COLOR="Blue"]bezoekerArray.get(positie).getKaartNummer()[/COLOR] ) ) 
    				{
    				    System.out.println( "gelijk"  + bezoekerArray.get(positie));
    				}
    			else
    				System.out.println( "ongelijk ");						
    		}		
    	}
    Consider this alternative (I really like the for-each style loop available since Java 5):
    Java Code:
    	public void checkOut (String vertrekNummer, int vertrektUur, int vertrekMinuten)
    	{
    		for (MuseumBezoeker bezoeker : bezoekerArray)
    		{
    			if ( vertrekNummer.equals(bezoeker.getKaartNummer())
    				{
    				    System.out.println( "gelijk"  + bezoeker);
    				}
    			else
    				System.out.println( "ongelijk ");						
    		}		
    	}
    -Gary-
    Last edited by gcalvin; 12-09-2010 at 05:42 PM. Reason: fixed class name and println statement

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,439
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by warchieflll View Post
    Eeh.. well, actually a 'bezoeker' (a object) is in the array. A 'bezoeker' is made of a cardname (kaartNummer, the String), and a time (a int for hours, and a int for minutes).

    Now I simply have to get the cardname (kaartNummer), out of the string from every position, and compare it to the last entered value in a textfield...

    So well, yea the string is in the same array as the two ints, but that is because it is together one object...
    That is AGT (A Good Thing (tm)); for a moment you scared me ;-) Gary already answered you question so you have your answer now.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    warchieflll is offline Member
    Join Date
    Dec 2010
    Posts
    55
    Rep Power
    0

    Default

    Hehe yea, Garry: your great! Only a ')' was missing, but you sloved my problem :D. I tried the for-each before, but didn't work. Now I see I just had to make a new method with a return... xD! Ty very much :D!!

    Java Code:
    	public void checkOut (String vertrekNummer, int vertrektUur, int vertrekMinuten)
    	{
    		for (MuseumBezoeker bezoeker : bezoekerArray)
    		{
    			if ( vertrekNummer.equals(bezoeker.getKaartNummer()))
    				{
    				    System.out.println( "gelijk"  + bezoeker);
    				}
    			else
    				System.out.println( "ongelijk ");						
    		}		
    	}

Similar Threads

  1. Replies: 12
    Last Post: 11-26-2010, 08:33 AM
  2. Replies: 1
    Last Post: 11-04-2010, 06:53 PM
  3. Read txt file into arraylist
    By nickerhardt in forum New To Java
    Replies: 9
    Last Post: 08-04-2010, 04:34 PM
  4. how to read the ArrayList inside HashMap
    By koddy in forum New To Java
    Replies: 6
    Last Post: 07-15-2010, 01:41 PM
  5. Read txt file to arrayList
    By koddy in forum New To Java
    Replies: 14
    Last Post: 04-29-2010, 05:15 PM

Posting Permissions

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