Results 1 to 5 of 5
- 10-22-2011, 08:15 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Why is this for loop not initiating?
Hello, I'm new here and had a quick question. I have set up output checks and it looks like my program stops right at the for loop declaration and does not progress into the loop, instead it seems to skip over it.
Anyone have any clue why this would be? Thank you!
Java Code:System.out.println("Enter Subscriber's name: "); subSearch = keyboard.nextLine().toUpperCase(); for(int i = 0; i < set.size(); i++) { anElement = set.getCurrent(); classId = anElement.getClassName(); if(classId.equals("Subscriber")) { aSub = (Subscriber) anElement; if((aSub.getName()).equals(subSearch)) { set.flagIt(aSub); } } }
- 10-22-2011, 08:36 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
Re: Why is this for loop not initiating?
An excellent opportunity for System.out.println( ... ) the Poor Man's Debugger (tm). Sprinkle in some of those statements and print out everything that is of interest to you in in he body of the for loop and see for yourself.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 10-22-2011, 08:43 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Re: Why is this for loop not initiating?
I tried this:
Result:Java Code://flag a specified subscriber System.out.println("Enter Subscriber's name: "); subSearch = keyboard.nextLine().toUpperCase(); System.out.println("Here."); for(int i = 0; i < set.size(); i++) { System.out.println("Here."); anElement = set.getCurrent(); System.out.println("Here."); classId = anElement.getClassName(); System.out.println("Here."); if(classId.equals("Subscriber")) { System.out.println("Here."); aSub = (Subscriber) anElement; System.out.println("Here."); if((aSub.getName()).equals(subSearch)) System.out.println("Here."); { System.out.println("Here."); set.flagIt(aSub); System.out.println("Here."); } } }
It looks like this is the culprit:Java Code:Here. Here. Here. Here.
Hmmm, now I'm even more confused. Our instructor provided us with the getClassName function. So I am unsure if it is doing it's job correctly.Java Code:if(classId.equals("Subscriber")) { aSub = (Subscriber) anElement; if((aSub.getName()).equals(subSearch)) { set.flagIt(aSub); } }
Here it is:
Java Code:public String getClassName() { String resultStr; int location; resultStr = this.toString(); location = resultStr.indexOf('@'); return resultStr.substring(0, location); }
- 10-22-2011, 09:49 PM #4
Member
- Join Date
- Oct 2011
- Posts
- 24
- Rep Power
- 0
Re: Why is this for loop not initiating?
I figured it out. The if statement was causing issues and was not even necessary. Here's the revised code:
Java Code:System.out.println("Enter Subscriber's name: "); subSearch = keyboard.nextLine().toUpperCase(); for(int i = 0; i < set.size(); i++) { anElement = set.getCurrent(); aSub = (Subscriber) anElement; if((aSub.getName()).equals(subSearch)) { set.flagIt(aSub); } else { System.out.println("Member not found."); } }
- 10-22-2011, 10:20 PM #5
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,410
- Blog Entries
- 7
- Rep Power
- 17
Similar Threads
-
Is it Possible? Array elements Initialized in Loop, can it be viewed outside loop?
By JPH in forum New To JavaReplies: 1Last Post: 10-01-2011, 02:12 AM -
JTextField loop 2x for-loop WEIRD!
By Streetproject in forum AWT / SwingReplies: 2Last Post: 02-16-2011, 05:46 PM -
Initiating a JFrame form
By ZaabZ in forum New To JavaReplies: 5Last Post: 08-28-2010, 08:15 PM -
How can I rewrite the following while loop using a for loop?
By gt11990 in forum New To JavaReplies: 5Last Post: 04-30-2010, 05:05 PM -
initiating objects question(kinda)
By helpisontheway in forum New To JavaReplies: 4Last Post: 01-11-2010, 03:07 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks