In: Computer Science
5.
Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the
problem from using arrays to ArrayLists. There are some other modifications as well, so read the instructions carefully.
The program should do the following:
–Ask the user for how many lines of text they wish to enter
–Declare and initialize an **ArrayList** of Strings to hold the user’s input
–Use a do-while loop to prompt for and read the Strings (lines of text) from the user
at the command line until they enter a sentinel value
–After the Strings have been read, use a for loop to step through the ArrayList of
Strings and concatenate a number to the beginning of each line eg:
This is change to 1 This is
The first day change to 2 The first day
and so on…
–Finally, use a **for each** loop to output the Strings (numbered lines) to the command
line.
*/
Below is L09 in-class assignment for part 5
2. Create a new program. This is the program you will submit for points today.
Write a Java code snippet to do the following:
– Declare and allocate an ArrayList of Strings
– Use a do-while loop to prompt for and read the Strings (lines of
text)
from the user at the command line until they enter a sentinel
value.
- Use a for loop to step through the ArrayList and concatenate a
line number
to the beginning of each line eg:
This is change to 1 This is
The first day change to 2 The first day
and so on…
– Finally, use a for loop to output the Strings (numbered lines) to the command line.
*/
Scanner scnr = new Scanner(System.in);
System.out.println("How many lines of text do you want to
enter?");
int numLines = 0;
numLines = scnr.nextInt();
System.out.println();
String [] arrayLines = new String[numLines];
scnr.nextLine();
int i = 0;
while(i < numLines)
{
System.out.println("Enter your text: ");
String text = scnr.nextLine();
arrayLines[i] = text;
i++;
}
for(i = 0; i < numLines; i++)
{
arrayLines[i] = (i + 1) + " " + arrayLines[i];
}
for(String element: arrayLines)
{
System.out.println(element);
}
}
}
code:
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
System.out.println("How many lines of text do you want to
enter?");
char b='y'; //to continue do while loop
int numLines = scnr.nextInt(); //reading number values to add to
arraylist
System.out.println();
ArrayList<String> list=new
ArrayList<String>(numLines);//Creating arraylist //creating
arraylist
scnr.nextLine();
int j=0;
do
{
System.out.println("Enter your text: ");
String text = scnr.nextLine(); //reading line of text
if(text.equals("q")) //this to sentinal terminate to read the
values
break;
list.add(text); //adding element to list
j++; //count purpose how many are adding
}
while(b=='y');
String a;
for(int i = 0; i < j; i++)
{
a=list.get(i); //to get values of arraylist
a=(i+1)+" "+a; //concatnating number before it
list.set(i,a); //setting the value again
}
for(String element: list) //printing
System.out.println(element);
}
}
OUTPUT:
How many lines of text do you want to enter?
5
Enter your text:
ramesh
Enter your text:
rajesh
Enter your text:
suresh
Enter your text:
venkat
Enter your text:
q
1 ramesh
2 rajesh
3 suresh
4 venkat
screenshot: