Question: Inside main(), add a statement to define an array named names that is suitable for holding a list of six (6) names. Compile your code
- Inside main(), add a statement to define an array named names that is suitable for holding a list of six (6) names.
- Compile your code to make sure it has correct syntax.
- Assign values to the first 3 array elements by inventing some names, like so:
names[0] = "George Washington";
- Assign values to the first 3 array elements by inventing some names, like so:
- Next, place a print statement below these 3 assignment statements:
System.out.println("The first 3 names:");
- Then, add a for-loop to display all three array values.
- Next, we are going to ask the user to enter the final 3 names to store in the array.
- Declare a Scanner variable at the top of your program.
- Add a second for loop to read in the next 3 names from the user input into the array:
System.out.println();
for (int i = ???; i < array.????; i++) {
System.out.print("Enter name " + (i + 1) + ": ");
array[i] = ???;
}
- Write a last for loop to display all the names in the array.
System.out.println(" The 6 names are: ");
//write your for loop here
- When you are finished, run your code and make sure that you are getting output similar to the following (except the names might differ):
The first 3 names:
George Washington
John Adams
Thomas Jefferson
Enter name 4: Grace Hopper
Enter name 5: Ada Lovelace
Enter name 6: Hedy Lamarr
The 6 names are:
George Washington
John Adams
Thomas Jefferson
Grace Hopper
Ada Lovelace
Hedy Lamarr
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
