Mr Man
Mr Man

Reputation: 1588

Using an array in a do....while statement

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.

Upvotes: 1

Views: 12829

Answers (6)


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"));

    }
}

Upvotes: -1

MAK
MAK

Reputation: 26586

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.

Upvotes: 5

Brandon Poole
Brandon Poole

Reputation: 372

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

Upvotes: -1

Hot Licks
Hot Licks

Reputation: 47729

int i = 0;
if (studentArray.length > 0) {
    do {
        doSomethingWithArrayElement(studentArray[i]);
        i++;
    } while (i < studentArray.length);
}

Upvotes: 1

Makoto
Makoto

Reputation: 106430

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

Upvotes: 4

Aravind Yarram
Aravind Yarram

Reputation: 80176

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

Upvotes: 4

Related Questions