Question

In: Computer Science

Write a java program: Write a nested loop program creating an array list of three positions.  The...

Write a java program:

Write a nested loop program creating an array list of three positions.  The first loop terminates with a sentinel value of which user is prompted for input to continue of quit.   Inside loop to enter in each position a prompted input of person's first name.  After the inside loop, edit the second array location making its value "Boston". Print the array's contents.

Solutions

Expert Solution

//JAVA CODE

import java.util.ArrayList;
import java.util.Scanner;

class MyArrayList
{
public static void main(String args[])
{
//create an Array List of size 3
ArrayList<String> names=new ArrayList<String>(3);
//create a scanner object
Scanner sc=new Scanner(System.in);   
//temporary variable to store first name
String s="";
//stores the user choice to continue or not
char choice;
//outer loop
while(true)
{
//inner loop
for(int i=0;i<3;i++)
{
System.out.println("\n Enter person first name :");
//accepts a name from user
s=sc.next();
//adding to array list
names.add(s);
}
//display the array list
System.out.println("Actual List :"+ names);

//replace the element at second position as "Boston"
names.set(1,"Boston");

//display the array list
System.out.println("Actual List :"+ names);
System.out.println("\n Do you want to continue (y/n) :");   
//accept the user choice to continue or not
choice=sc.next().toLowerCase().charAt(0);
//clear the list
names.clear();

if(choice=='n')
break;   
}
}
}

OUTPUT


Related Solutions

Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
IN JAVA Create a program that uses a nested for loop to display a rectangle of...
IN JAVA Create a program that uses a nested for loop to display a rectangle of #'s with a given number of rows and columns,. This should only display the # when the column is an odd number (see examples below). Get the number of rows and columns from the user, and display the result. Examples: If the user provided rows=4, and columns=7, the program should display a pattern as follows: # # # # # # # # #...
Write a Java program that reads a list of integers into an array. The program should...
Write a Java program that reads a list of integers into an array. The program should read this array from the file “input.txt”. You may assume that there are fewer than 50 entries in the array. Your program determines how many entries there are. The output is a two-column list. The first column is the list of the distinct array elements; the second column is the number of occurrences of each element. The list should be sorted on entries in...
In Java: Write a nested loop that allows the user to continuously enter phrases and output...
In Java: Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case). Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers....
Write a java program creating an array of the numbers 1 through 10. Shuffle those numbers. Randomly pick a number between one and ten and then sequentially search the array for that number. Once the number is found, place that number at the top of the list. Print the list. Perform #3 thru #5 ten times.
JAVA Write nested while loop that will print out this pattern, based upon a number entered...
JAVA Write nested while loop that will print out this pattern, based upon a number entered by the user. User enters 4: 1234 1234 1234 1234 User enters 2: 12 12
JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing...
JAVA!!!! int[][] BingoCardArray = new int[5][5]; Use a nested for-loop, to populate the 2-D array representing 5 columns for B – I – N – G – O, where row 1 =         col 1 = random # 1- 15         col 2 = random # 16 – 30         col 3 = random # 31 – 45         col 4 = random # 46 – 60         col 5 = random # 61 – 75 row 2 =         col 1 = random # 1-...
Write a program in java that deliberately contains an endless or infinite while loop. The loop...
Write a program in java that deliberately contains an endless or infinite while loop. The loop should generate multiplication questions with single-digit random integers. Users can answer the questions and get immediate feedback. After each question, the user should be able to stop the questions and get an overall result. See Example Output. Example Output What is 7 * 6 ? 42 Correct. Nice work! Want more questions y or n ? y What is 8 * 5 ? 40...
Write a Java program that creates a three-dimensional array. Populate each element with a string that...
Write a Java program that creates a three-dimensional array. Populate each element with a string that states each coordinate position in the array.
Language: c++ works in visual basic Write a program that uses an array of nested structs...
Language: c++ works in visual basic Write a program that uses an array of nested structs to store the addresses for your store’s customers.  Each customer has a name and two addresses: home address and business address.  Each address has a street, city, state, and zip code. Requirements: 1. Data structure a. Define an Address struct with street, city, state and zip fields b. Define a Customer struct with lastNm and firstNm fields, plus homeAddr and busAddr fields...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT