Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Looping Over and Over Again?

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Thanked 0 Times in 0 Posts

    Question Looping Over and Over Again?


    So I have this basic mail forwarding program and I'm running into a big issue with regards to the loop that I'm using.

    String name = input.next();
    String mailingAddress = input.next();
    iterator = entries.iterator();
    for (int i = 0; i < entries.size(); i++)
    String[] otherEntryInfo = (iterator.next().split(";"));
    String otherName = otherEntryInfo[0];
    String oldAddress = otherEntryInfo[1];
    String newAddress = otherEntryInfo[2];
    if (name.equals(otherName) && mailingAddress.equals(oldAddress))
    mailingAddress = newAddress;
    System.out.println("Send to " + mailingAddress);

    input.next() is a Scanner that reads the next user input
    entries is a linked list of Strings that have the format name;oldAddress;newAddress
    iterator.next() gives the next String in the linked list

    So the issue is that this works for entries where the program would only have to forward the mail from one address to the next, but does not work when it has to do this more than once. I realize that the order I enter the Strings into my linked list can also affect whether my program will realize to forward the mail D: I was thinking of placing this for loop inside another for loop so that it runs more times, but I feel like there's a better solution.

    Any help would be appreciated!

    EDIT: Placing the code inside another for loop DOES work, but I'm curious if there's a better way to do this.
    Last edited by avalanche72; February 1st, 2012 at 04:55 AM.

  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    My Mood
    Thanked 82 Times in 78 Posts
    Blog Entries

    Default Re: Looping Over and Over Again?

    but does not work when it has to do this more than once.
    How does it behave then?
    1. Welcome to Java Programming Forums.
    2. Don't forget to read the Forums Rules.
    3. Wrap your code in code tags.
    4. Please provide an SSCCE

Similar Threads

  1. Need help with looping!
    By crsoccerplayer6 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 29th, 2011, 04:09 PM
  2. Looping an inputstream
    By Trunk Monkeey in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 25th, 2011, 02:39 PM
  3. Help in looping
    By endframe in forum What's Wrong With My Code?
    Replies: 5
    Last Post: December 28th, 2010, 03:24 PM
  4. looping method
    By dcshoecousa in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 5th, 2010, 09:19 PM
  5. [SOLVED] looping, for,while,do-while.
    By chronoz13 in forum Loops & Control Statements
    Replies: 4
    Last Post: August 6th, 2009, 01:32 PM