View the step-by-step solution to:

Question

How to convert below for loop to while loop.....I am working on while loops and the assignment question is "Create

a method called **public String[] getProvincesWhoseNameContains(String substring)** which returns an array of the names of all provinces whose name contains substring (the parameter). Hint: use the String class's conatins() method. If parameter is null or empty throw and IllegalArgumentException.


I was able to find for loop on your website but not sure how to do while loop as that is what we are working on now. Here is the picture of for loop from your site


  public String[] getProvinceWhoseNameContains(String substring) 

  {

    String[] answers = new String[13]; 

    String provinceNames = "";

    int a = 0;

     

    for (int i = 0; i < provinces.length; i++) 

    {

      provinceNames = provinces[i].getName();

      if (provinceNames.contains(substring)) 

      {

        provinceNames = answers[a];

        a++;

      }

    }

    return answers;

  }

Top Answer

The code in while loop is given... View the full answer

Sign up to view the full answer

Why Join Course Hero?

Course Hero has all the homework and study help you need to succeed! We’ve got course-specific notes, study guides, and practice tests along with expert tutors.

  • -

    Study Documents

    Find the best study resources around, tagged to your specific courses. Share your own to gain free Course Hero access.

    Browse Documents
  • -

    Question & Answers

    Get one-on-one homework help from our expert tutors—available online 24/7. Ask your own questions or browse existing Q&A threads. Satisfaction guaranteed!

    Ask a Question