1

I am trying to use a do...while loop that loops based on the id's in my array. I am a little new to using the do while loop, so am having some trouble incorporating the array into the thing. Here is my relevant code:

    String studentId   = StringUtils.defaultString(request.getParameter("Student_ID"));
    String studentId1  = StringUtils.defaultString(request.getParameter("Student_ID1"));   
    String studentId2  = StringUtils.defaultString(request.getParameter("Student_ID2"));
    String studentId3  = StringUtils.defaultString(request.getParameter("Student_ID3"));
    String studentId4  = StringUtils.defaultString(request.getParameter("Student_ID4"));
    String studentId5  = StringUtils.defaultString(request.getParameter("Student_ID5"));
    String studentId6  = StringUtils.defaultString(request.getParameter("Student_ID6"));
    String studentId7  = StringUtils.defaultString(request.getParameter("Student_ID7"));
    String studentId8  = StringUtils.defaultString(request.getParameter("Student_ID8"));
    String studentId9  = StringUtils.defaultString(request.getParameter("Student_ID9"));


    String[] studentArray;
    studentArray = new String [15];
        studentArray[0]  = studentId;          studentArray[1]  = studentId1;
        studentArray[2]  = studentId2;         studentArray[3]  = studentId3;
        studentArray[4]  = studentId4;         studentArray[5]  = studentId5;
        studentArray[6]  = studentId6;         studentArray[7]  = studentId7;
        studentArray[8]  = studentId8;         studentArray[9]  = studentId9;


do {
    // blah blah blah
} while (  // Here is where I want to tell it to loop for every student in my array.  It should not run for id's that are empty strings(*if possible));

See comments for a more clear explanation. The StringUtils.defaultString that is used when i am getting my parameters will give me an empty string if the param receives a NULL. So if possible I would like to take that into account, and not run my "do {}" statements when the particular array ID is an empty string. Thanks in advance for the help, and if you have any questions, please ask.

5
  • 1
    Why specifically a do...while loop? This could easily be done with a foreach loop Commented Jan 3, 2012 at 21:44
  • It would be better to use a for loop here. Commented Jan 3, 2012 at 21:45
  • @fge yes, I believe that a for each loop would work as well, I was just curious how a do while might work, but using an array in for each loop is still a problem for me. Commented Jan 3, 2012 at 21:46
  • 1
    studentArray.length will tell you how many elements are in the array. It's easier to iterate over the array with for (int i = 0; i < studentArray.length; i++), but you can simulate that with do/while if you keep and "manually" increment/test your own index variable. Commented Jan 3, 2012 at 21:47
  • @HotLicks thank you for the tips, it makes since to do a for loop now that i see it written out, Idk why the array was tripping me up so much Commented Jan 3, 2012 at 21:49

6 Answers 6

5

This is better done using a for each loop:

for(String x:studentArray){
  // do what you want to do with x
}

To skip over values that are null, just insert an if check at the beginning of the loop and continue if x is empty.

Sign up to request clarification or add additional context in comments.

1 Comment

@mak - 'x' can never be null as the OP is pre-processing it already
4

Using enhanced for loop is simple

for(String student: studentArray)
{
  if(!student.equals(""))
  {
    //process data
  }
}

NOTE: The if condition can also be written as !"".equals(student) but it is not necessary in your scenario as you are already making sure the id's are never null by using StringUtils.defaultString

Comments

4

The only difference between a do...while loop and a while loop is that the former is always executed at least once.

With that in mind, we can write code as if we were writing a standard while loop, keeping in the back of our minds that this loop will run through at least once.

int i = 0; // Some counter to keep track of the index position
do {
    if(!(studentArray[i] == null || studentArray[i] == "")) {
        // Good stuff here
    }
    i++;
} while(i < studentArray.length);

2 Comments

You forgot to check if the array is empty.
If one initializes the array, at worst its base value will be "". If one assigns values into the array, at worst its base value will be null, 0, or false. I work form the assumption (and intent of the original code) that the array is not empty, but you are otherwise absolutely right.
1
int i = 0;
if (studentArray.length > 0) {
    do {
        doSomethingWithArrayElement(studentArray[i]);
        i++;
    } while (i < studentArray.length);
}

Comments

-1
        string[] arr = new string[4]; // Initialize
        arr[0] = "one";               // Element 1
        arr[1] = "two";               // Element 2
        arr[2] = "three";             // Element 3
        arr[3] = "four";              // Element 4

        // Loop over strings
        foreach (string s in arr)
        {
            if (!String.IsNullOrEmpty(s))
            {
                Console.WriteLine(s);
            }
        }

        // do while
        var aValue = "Not Empty";
        do
        {
            //loop through elements here
            for (int a = 0; a < arr.Length; a++)
            {
                aValue = arr[a];
            }

        } while (!String.IsNullOrEmpty(aValue));

1 Comment

C# may be like Java, but it isn't exactly Java. It also seems that you've missed the point of the do...while; it seems that, if the last element is empty in the array, it will terminate, but not a step before.
-1

This a sample code using arrays This Program Uses Do.... While Loop to Loop over a whole program when there is a user prompt.


package doWhileLoop;

import java.util.Scanner;

public class doWhileLoop {
    public static void main(String[] args) {
        //this is a program to prompt a user to continue or pass using the do while loop
        String programCounter;
        do {
            int sum=0;
            int list[] = new int[3];
            Scanner in = new Scanner(System.in);
            System.out.println("Enter  3 numbers to be added: ");
            for (int i = 0; i < 3; i++) {
                list[i] = in.nextInt();
                sum+= list[i];
            }
            System.out.println("sum = "+ sum);

            System.out.println("Enter Yes to continue or No to exit........");
            programCounter = in.next();

        }
        while (programCounter.equals("yes"));

    }
}

1 Comment

Please add some explanation to your answer - how is this related to the given question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.