Results 1 to 7 of 7
- 12-09-2010, 05:12 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
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 "); } }
- 12-09-2010, 05:17 PM #2
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?
- 12-09-2010, 05:17 PM #3
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
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,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 12-09-2010, 05:26 PM #4
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
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...
- 12-09-2010, 05:38 PM #5
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
I think you mean to do this:
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 (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 "); } }
-Gary-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 "); } }Last edited by gcalvin; 12-09-2010 at 05:42 PM. Reason: fixed class name and println statement
- 12-09-2010, 05:47 PM #6
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,405
- Blog Entries
- 7
- Rep Power
- 17
- 12-09-2010, 05:58 PM #7
Member
- Join Date
- Dec 2010
- Posts
- 55
- Rep Power
- 0
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
-
Binary-algorithm -> Insert String to sorted String-ArrayList
By Muskar in forum Advanced JavaReplies: 12Last Post: 11-26-2010, 08:33 AM -
Test for all empty Strings in LinkedHashMap<String,ArrayList<String>
By albertkao in forum New To JavaReplies: 1Last Post: 11-04-2010, 06:53 PM -
Read txt file into arraylist
By nickerhardt in forum New To JavaReplies: 9Last Post: 08-04-2010, 04:34 PM -
how to read the ArrayList inside HashMap
By koddy in forum New To JavaReplies: 6Last Post: 07-15-2010, 01:41 PM -
Read txt file to arrayList
By koddy in forum New To JavaReplies: 14Last Post: 04-29-2010, 05:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks