Page 1 of 2 12 LastLast
Results 1 to 20 of 31
  1. #1
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Exclamation Problems with a loop calling data from an ArrayList.

    I'm writing a program built to store, retrieve and sort data for assignments. I've got most of the code down fine, I've got a seperate Assignment class with .get and .set methods, and everything has been working fine, but in my main program, I'm trying to remove an entry. The program seems to just skip over all the code I've got to remove it.

    aryAssignments is my ArrayList containing in each node an Assignment object containing a string and number
    int temp = 0;
    while(temp < (aryAssignments.size() -1)){
    if(strName == (aryAssignments.get(temp)).getAssnName()){

    aryAssignments.remove(temp);
    System.out.println("All instances of \"" + strName + "\" were found and erased.");

    }
    else{
    System.out.println("\"" + strName + "\" was NOT found.");
    }
    temp++;
    }
    This is all under a switch which is clearly getting called (I put in a .println("test"); at the very end of that switch option) but it seems to skip over everything straight to the break;

    Anyone have any ideas?

    Btw, I originally did this with a for loop, but switched to the while just to see if it'd work. I'm pulling out my hair on this one! Everything else is working fine!

    I can provide any additional code if you need it, but I don't believe you'll need it, and I just want a good nudge in the right direction.
    Last edited by moriarty; 03-27-2010 at 11:23 PM. Reason: additional information

  2. #2
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Another question while I'm here, is there a way I can "watch" local variables in eclipse while running my main program? It'd sure help in a situation like this instead of including hidden method calls just to view a temporary variable.

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

    Default

    Java Code:
            if(strName == (aryAssignments.get(temp)).getAssnName()){
    Don't use == to compare Strings.

    -Gary-

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If the body of the while block is not being executed (is this what you mean by "skipping over everything straight to the break"?) then aryAssignments.size() is zero or one.

  5. #5
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Gary: What should I be doing instead of using ==?

    pbrock: Yes, the body of the while block isn't being executed, neither the if or else statements.

    I'll check and see what aryAssignments.size() is reporting.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    If you think the size() should be bigger than 1, then you'll need to go back to wherever you thought it should have got that larger value and see why that didn't happen.

    [Edit]

    Use someStr.equals(otherStr) for comparing Strings (and other objects).

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

    Default

    Quote Originally Posted by moriarty View Post
    Gary: What should I be doing instead of using ==?
    Use either the equals(...) or equalsIgnoreCase(...) method.

  8. #8
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    If I add more than 1 element to the ArrayList, it'll shoot out the else ("...not found")

    I suspect Gary is right and that I'm comparing the strings incorrectly.

    Edit: Thanks Fubarable, I'm using equalsIgnoreCase now, but still getting the same results. I've got enough parentheses in that line to make my head spin >_< I'll go over it closely
    Last edited by moriarty; 03-27-2010 at 11:35 PM. Reason: bad timing

  9. #9
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Less than one error, I believe. ><

    Working back on it, thanks guys.

    EDIT: Yep, that was it. Just removed the -1 in the statement. Go figure, ID10T error on my part. :P
    Last edited by moriarty; 03-27-2010 at 11:38 PM. Reason: Fixed

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    I've got enough parentheses in that line to make my head spin >_<
    There's no extra charge for using local variables!

    Edit: I'm glad you've got it sorted out.
    Last edited by pbrockway2; 03-27-2010 at 11:42 PM.

  11. #11
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Quote Originally Posted by pbrockway2 View Post
    There's no extra charge for using local variables!
    :D

    How can something so infuriating be so intriguing?

    ...damn you java...

  12. #12
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    It doesn't seem to want to remove the node, but at least it's trying to now. I'm going to keep playing with it, if anyone has a suggestion it'd be helpful.

    Also, my "not found" line is logically austistic. It displays it every time if it isn't found, there a quick fix for that or should I make a boolean showing if it was found or not?

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

    Default

    You can remove() either by index or by value, so your code might be easier to understand if you did it this way:
    Java Code:
        boolean nameFound = false;
        for (Assignment assignment : aryAssignments) { // I think "assignments" is a better name for the ArrayList, btw
            if (strName.equals(assignment.getAssnName()) {
                aryAssignments.remove(assignment);
                nameFound = true;
            }
        }
        if (nameFound) {
            System.out.println("All instances of \"" + strName + "\" were found and erased.");
        } else {
            System.out.println("\"" + strName + "\" was NOT found.");
        }
    EDIT: Sorry, don't try this. Apparently I was misinformed or misunderstood. It is NOT safe to remove elements while doing for-each iteration. More soon.

    -Gary-
    Last edited by gcalvin; 03-27-2010 at 11:58 PM.

  14. #14
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Java Code:
    					case 2: {//Remove an assignment							
    						System.out.println("\nPlease enter the assignment name:");
    						String strName = keyboard.next();
    
    						for(int j = 0; j < (aryAssignments.size()); j++){
    							if(strName.equalsIgnoreCase((aryAssignments.get(j)).getAssnName())){
    								aryAssignments.remove(j);
    								System.out.println("All instances of \"" + strName + "\" were found and erased.");
    							}
    							else{
    								System.out.println("\"" + strName + "\" was NOT found.");
    							}
    							
    						}
    
    					break;}
    Edit: just saw the last post, i'm going to look over it, but just posting my current code for reference.

  15. #15
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    for (Assignment assignment : aryAssignments)

    is the only part where I'm a bit lost, I haven't used syntax like that before. Could you explain it?

    By the way, you guys are a fantastic help, I'm learning a lot from this error. :P

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

    Default

    Sorry, looks like I was mistaken. Give me a minute and I'll post more.

    -Gary-

  17. #17
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    EDIT: Sorry, don't try this. Apparently I was misinformed or misunderstood. It is NOT safe to remove elements while doing for-each iteration. More soon.

    -Gary-
    Thank Java for comment blocks. :D

    also, I got in a habit of putting a short little note of what the varibles type in high school, haven't kicked it :P
    ex: intInteger lngLong strString, etc

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

    Default

    OK, here's how you really want to do it.
    Java Code:
        boolean nameFound = false;
        for (Iterator<Assignment> iter = aryAssigments.iterator(); iter.hasNext(); ) { // note empty third part
            if (strName.equals(iter.next().getAssnName()) {
                iter.remove();
                nameFound = true;
            }
        }
        if (nameFound) {
            System.out.println("All instances of \"" + strName + "\" were found and erased.");
        } else {
            System.out.println("\"" + strName + "\" was NOT found.");
        }
    Iterator (Java Platform SE 6)

    And, for reference,
    The For-Each Loop
    but you can't use for-each if you're going to be removing elements.

    -Gary-
    Last edited by gcalvin; 03-28-2010 at 12:36 AM.

  19. #19
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    Hmm, it seems it won't take .getIterator, but I've tried .iterator and .listIterator and it looked like .listIterator worked, but only in the case of 2 objects with identical names.

    I'll go over the javadoc

  20. #20
    moriarty is offline Member
    Join Date
    Mar 2010
    Posts
    24
    Rep Power
    0

    Default

    It seems that it only kicks in if the last element of the ArrayList is what it's looking for, and then it clears the whole array back to a size of 0.

Page 1 of 2 12 LastLast

Similar Threads

  1. Calling a method when using an arraylist?
    By Jamison5213 in forum New To Java
    Replies: 10
    Last Post: 01-23-2010, 08:47 PM
  2. ArrayList problems
    By komo225 in forum New To Java
    Replies: 4
    Last Post: 02-12-2009, 04:14 AM
  3. [Problems] ArrayList
    By Zuela in forum New To Java
    Replies: 1
    Last Post: 06-16-2008, 11:51 AM
  4. Iterating through ArrayList using For loop
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 01-20-2008, 08:53 AM
  5. Replies: 0
    Last Post: 11-14-2007, 03:22 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
  •