The purpose of this assignment is to prove that you can build and use an arrayli
ID: 3815931 • Letter: T
Question
The purpose of this assignment is to prove that you can build and use an arraylist.
Create a project named LastnameFirstnameHW7.
The purpose of your program is show that you can build and use an arraylist. The program will function a lot like Homework 6. The program should operate as follows:
Enter a name or 'Q' to quit: Tamaya
Enter a name or 'Q' to quit: Doug
Enter a name or 'Q' to quit: Lakshmi
Enter a name or 'Q' to quit: Q
The names you entered are: Tamaya, Doug, Lakshmi.
You must use a loop and an arraylist for this program. Assignments submitted without a loop and an arraylist will not be graded.
DO NOT use an array to complete this progam. You must create and use an arraylist.
Hint: it is easier to use a While loop than a For loop for this assignment.
Hint: you can use a boolean variable (see ArrayDemo.java example code) to keep track of whether to iterate your loop again
Explanation / Answer
import java.util.*;
import java.io.*;
class Test{
public static void main(String args[]){
ArrayList<String> a_list=new ArrayList<String>();
Scanner sc=new Scanner(System.in);
System.out.println("Enter a name or 'Q' to quit:");
String s=sc.next();
if(s.equals("Q"))
{
System.exit(0);
}
while(!s.equals("Q"))
{
a_list.add(s);
System.out.println("Enter a name or 'Q' to quit:");
s=sc.next();
}
System.out.println("The names you entered are: ");
Iterator itr=a_list.iterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.