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 3 of 3

Thread: While 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

    Question While loop only runs one time?

    I have a arrayList with multiple entries.
    And I would like to grab the first entry and the look in a file and grab a certain line.
    After that I would like to grab the second entry from the arrayList and look that one up in a file.

    Now I made this code for it:

    for (int i = 0; i < arrayList_Code_Sorted.size(); i++) {
    				String code = arrayList_Code_Sorted.get(i);
     
    				//
    				//
    				while (sc.hasNextLine()) {
    					// Grab very line separate. 
    					String line = sc.nextLine(); 
    					// Split the line by ever "tab". 
    					String value[] = line.split("\t");
    					// Get start  
    					String start = value[2];
    					// Get end 
    					String end = value[3];
    					// Get strand 
    					String strand = value[1];
     
    					String possible = start + "_" + end + "_" + strand;
     
    					//System.out.println(line);
     
    					//
    					if (possible.equals(code)) {
    						//
    						System.out.println(line);
    					}
     
    				}
    			}

    I expected this to do what I would like.
    But it only looks up the first value of the arrayList in the file.
    And that is it.

    Why doesn't it do the while loop more often?

    I know the for loop works, but the while loop only gets activated the first time...


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: While loop only runs one time?

    Hello Purple01!

    Firstly, It would help if you posted a complete sample of your code and file so we can test it.
    Have you stepped through your code with a debugger to see if the while loop executed?
    Add to your code an else block with a descriptive message. ie
    else
    {
       System.out.println(line +" doesn't equal code.");
    }

    I'm guessing the while loop is working. It is just that the if statement is evaluated to false after the first time and this happens because of the sc.hasNextLine(). Maybe it compares the value from the list with the same (the first) line of the file. Try printing both values (from the list and the file) to see what's going on.

    Hope this helps.

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

    Default Re: While loop only runs one time?

    I found the solution.
    Thanks for the help you gave.

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. For loop does not execute the first command after 2nd time
    By lina_inverse in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 16th, 2012, 08:41 PM
  3. Replies: 6
    Last Post: October 3rd, 2012, 06:39 PM
  4. Loop only runs one time?
    By Purple01 in forum Loops & Control Statements
    Replies: 6
    Last Post: September 14th, 2012, 06:57 AM
  5. 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