Results 1 to 5 of 5
  1. #1
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default 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);
                            }
                        }
                    }

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default 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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default Re: Why is this for loop not initiating?

    I tried this:

    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.");
                            }
                        }
                    }
    Result:

    Java Code:
    Here.
    Here.
    Here.
    Here.
    It looks like this is the culprit:

    Java Code:
    if(classId.equals("Subscriber"))
                        {
                            aSub = (Subscriber) anElement;
                            if((aSub.getName()).equals(subSearch))
                            {
                                set.flagIt(aSub);
                            }
                        }
    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.

    Here it is:

    Java Code:
    public String getClassName()
        { 
            String resultStr;
            
            int location;
            
            resultStr = this.toString();
            location = resultStr.indexOf('@');
            return resultStr.substring(0, location);
        }

  4. #4
    Scopic is offline Member
    Join Date
    Oct 2011
    Posts
    24
    Rep Power
    0

    Default 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.");
                            }
                    }

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,524
    Blog Entries
    7
    Rep Power
    20

    Default Re: Why is this for loop not initiating?

    See? The Poor Man's Debugger (tm) is of great value.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 1
    Last Post: 10-01-2011, 02:12 AM
  2. JTextField loop 2x for-loop WEIRD!
    By Streetproject in forum AWT / Swing
    Replies: 2
    Last Post: 02-16-2011, 05:46 PM
  3. Initiating a JFrame form
    By ZaabZ in forum New To Java
    Replies: 5
    Last Post: 08-28-2010, 08:15 PM
  4. Replies: 5
    Last Post: 04-30-2010, 05:05 PM
  5. initiating objects question(kinda)
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 01-11-2010, 03:07 AM

Posting Permissions

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