Results 1 to 20 of 31
- 03-27-2010, 11:18 PM #1
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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
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;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++;
}
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
- 03-27-2010, 11:26 PM #2
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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.
- 03-27-2010, 11:26 PM #3
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Don't use == to compare Strings.Java Code:if(strName == (aryAssignments.get(temp)).getAssnName()){
-Gary-
- 03-27-2010, 11:26 PM #4
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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.
- 03-27-2010, 11:28 PM #5
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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.
- 03-27-2010, 11:31 PM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
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).
-
- 03-27-2010, 11:31 PM #8
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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 closelyLast edited by moriarty; 03-27-2010 at 11:35 PM. Reason: bad timing
- 03-27-2010, 11:37 PM #9
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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. :PLast edited by moriarty; 03-27-2010 at 11:38 PM. Reason: Fixed
- 03-27-2010, 11:39 PM #10
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
There's no extra charge for using local variables!I've got enough parentheses in that line to make my head spin >_<
Edit: I'm glad you've got it sorted out.Last edited by pbrockway2; 03-27-2010 at 11:42 PM.
- 03-27-2010, 11:41 PM #11
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
- 03-27-2010, 11:47 PM #12
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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?
- 03-27-2010, 11:49 PM #13
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
You can remove() either by index or by value, so your code might be easier to understand if you did it this way:
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.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."); }
-Gary-Last edited by gcalvin; 03-27-2010 at 11:58 PM.
- 03-27-2010, 11:50 PM #14
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Edit: just saw the last post, i'm going to look over it, but just posting my current code for reference.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;}
- 03-27-2010, 11:52 PM #15
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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
- 03-27-2010, 11:59 PM #16
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
Sorry, looks like I was mistaken. Give me a minute and I'll post more.
-Gary-
- 03-27-2010, 11:59 PM #17
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Thank Java for comment blocks. :DEDIT: 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-
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
- 03-28-2010, 12:15 AM #18
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
OK, here's how you really want to do it.
Iterator (Java Platform SE 6)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."); }
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.
- 03-28-2010, 12:27 AM #19
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
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
- 03-28-2010, 12:31 AM #20
Member
- Join Date
- Mar 2010
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
Calling a method when using an arraylist?
By Jamison5213 in forum New To JavaReplies: 10Last Post: 01-23-2010, 08:47 PM -
ArrayList problems
By komo225 in forum New To JavaReplies: 4Last Post: 02-12-2009, 04:14 AM -
[Problems] ArrayList
By Zuela in forum New To JavaReplies: 1Last Post: 06-16-2008, 11:51 AM -
Iterating through ArrayList using For loop
By Java Tip in forum Java TipReplies: 0Last Post: 01-20-2008, 08:53 AM -
Iterating through ArrayList - traditional for loop
By Java Tip in forum Java TipReplies: 0Last Post: 11-14-2007, 03:22 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks