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.


>> REGISTER NOW TO START POSTING


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

Results 1 to 7 of 7

Thread: Loop only runs one time?

  1. #1
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Loop only runs one time?

    I have made a program that reads in an excel file. And then stores certain things in an arrayList and HashMap.
    Now I would like to compaire if an value from the arrayList is available in the HashMap.

    				// Get the keys from the HashMap, mapFile1. 
    				Set<Cell> keys = mapFile1.keySet(); 
    				//
    				Iterator<Cell> keyIterator = keys.iterator(); 
     
    				// Get the keys from the arrayList (ArrayListFileData2). 
    				for (int i = 0; i < ArrayListFileData2.size(); i++) { 
     
    					//System.out.print(ArrayListFileData2.get(i) + " ");
    					// Get the data out of the arrayList. 
    					Cell ArrayKey2 = ArrayListFileData2.get(i); 
    					//TEST LINE
    					//System.out.println(ArrayKey2);
     
    					//Change ArrayKey2 to a String.  
    					String ArrayKey2String = "" + ArrayKey2; 
     
    					while (keyIterator.hasNext()) {
    						Cell keyObj = keyIterator.next();
    						//System.out.println( "Key: " + keyObj);
     
    						String keyObjString = "" + keyObj;
    						//System.out.println("Hallo: " + keyObjString);
    						// Look if there is a match. 
    						if (ArrayKey2String.equals(keyObjString)) { 
    							System.out.println( "Key: " + keyObj);
    							System.out.println("Ja"); 
    							// Write matched keys to an arrayList. 
    							ArrayListOutputMatch.add(ArrayKey2String); 
    						}
    						else {
    							//System.out.println("Noting found");
    						}
    					} 
    				}

    What he does correct:
    - Grab every value from the arrayList seperate.
    - Grab every key from the LinkedHashMap seperate.

    My problem now is:
    It only fiends the first word (from the arrayList) in the HashMap.
    And not the other ones who come after that.

    It looks like the if (ArrayKey2String.equals(keyObjString)) is only being runned one time.
    And that is for the first value of the arrayList.

    How do I let it run too for the other values?

    If I let it print the lines:
    //System.out.print(ArrayListFileData2.get(i) + " ");
    and
    //System.out.println("Hallo: " + keyObjString);

    I get as output:
    Hallo: Index
    Key: Index
    Ja

    Hallo: 1.0
    Hallo: 2.0
    Hallo: 3.0
    Hallo: 4.0
    Hallo: poike
    Hallo: kvinna


    etc.

    So the purple part is made by the if loop.
    And the blue part is made in the while loop.

    But the other values are also in that HashMap.
    Why doesn't that show a match?
    Last edited by Purple01; September 14th, 2012 at 02:45 AM.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Loop only runs one time?

    Quote Originally Posted by Purple01 View Post
    Why doesn't that show a match?
    That, my friend, is the question. Why doesn't it? You have some opinions on what might be wrong. Add a few printlns to your code to verify your guesses.
    If you think a method is not being called enough, (or too often), println just before the call to it, and println just inside of the method body so you can see from where the method was called and the value(s) of any parameters.
    Print the index counter from inside a loop.
    Print the result of your boolean tests outside the if, even if that means you have to declare a local boolean just to get your test outside the if so you can see what the result is.
    Just think about what results you are getting, and what you think you should get, and print out everything involved. Usually something will stand out.

  3. The Following User Says Thank You to jps For This Useful Post:

    Purple01 (September 14th, 2012)

  4. #3
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Loop only runs one time?

    I see that "ArrayKey2String" is being stuck on "Index" inside the if loop.
    How do I know how to solve that?
    I tried ArrayKey2String = ""; behind the while loop but that wasn't it.
    Last edited by Purple01; September 14th, 2012 at 04:04 AM.

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Loop only runs one time?

    Quote Originally Posted by Purple01 View Post
    I see that "ArrayKey2String" is being stuck on "Index" inside the if loop.
    What do you mean by that?
    When should "Index" increment/decrement? What should cause the value of "Index" to change, and why is it not happening?

  6. #5
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Loop only runs one time?

    If I let print:

    System.out.print(ArrayListFileData2.get(i) + " ")

    Then I get everything one by one.

    So by doing this:

    Cell ArrayKey2 = ArrayListFileData2.get(i);

    I have in ArrayKey2 evey value seperated.
    For everytime the for loop is runned.

    And with:

    String ArrayKey2String = "" + ArrayKey2;

    I just convert Cell into a String.
    But apparently something goed wrong in that one (red one).
    But I don't understand why...
    Last edited by Purple01; September 14th, 2012 at 04:42 AM.

  7. #6
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Loop only runs one time?

    So if you think there is a problem with that line do something like this:
    println valueOfArrayKey2Before
    String ArrayKey2String = "" + ArrayKey2;
    println ArrayKey2String;
    See if you are getting what you expect. If you are, trace the program to the next point, and see what is going on the next time a decision is being made. The program is only so long, and if you follow step by step, you will see why the program is acting the way it is. Pretend you don't know what it is supposed to do, and step through it looking (possibly writing down) what it is doing.

  8. #7
    Member
    Join Date
    Sep 2012
    Location
    The Netherlands
    Posts
    84
    My Mood
    Inspired
    Thanks
    4
    Thanked 1 Time in 1 Post

    Default Re: Loop only runs one time?

    I will, thank you.

Similar Threads

  1. My code runs forever without output
    By jbinsomnia in forum What's Wrong With My Code?
    Replies: 6
    Last Post: October 8th, 2011, 07:53 AM
  2. How to control the time that a function takes to execute in a loop?
    By GodspeedPR in forum Loops & Control Statements
    Replies: 4
    Last Post: July 20th, 2011, 03:37 PM
  3. while(true){ section of code runs only once}
    By jack_nutt in forum Java Theory & Questions
    Replies: 5
    Last Post: June 19th, 2011, 06:15 PM
  4. Thread runs only once and not again
    By enflation in forum Threads
    Replies: 3
    Last Post: June 9th, 2010, 10:51 AM
  5. runs once only
    By silverspoon34 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: November 21st, 2009, 03:31 AM