Java Programming Create an array of Strings that contains th

Java Programming:

Create an array of Strings that contains the following:

     String[] names = { \"Bill\", \"Mike\", \"Joe\", \"Dave\", \"Sam\", \"Mary\", \"George\", \"Lucy\" }

In your main program, search the array and display all of the elements that begin with \'M\'.

Solution

NamesSearch.java


public class NamesSearch {

   public static void main(String[] args) {
       String[] names = { \"Bill\", \"Mike\", \"Joe\", \"Dave\", \"Sam\", \"Mary\", \"George\", \"Lucy\" };
       System.out.println(\"Display all of the elements that begin with \'M\': \");
       for(int i=0; i<names.length; i++){
           if(names[i].startsWith(\"M\")){
               System.out.println(names[i]);
           }
       }
   }
}

Output:

Display all of the elements that begin with \'M\':
Mike
Mary

Java Programming: Create an array of Strings that contains the following: String[] names = { \

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site