5. Modify the program for Line Numbers from L09: In-Class Assignment. Mainly, you are changing the problem from...

90.2K

Verified Solution

Question

Programming

5.

Modify the program for Line Numbers from L09: In-ClassAssignment. Mainly, you are changing the

problem from using arrays to ArrayLists. There are some othermodifications 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 theuser’s input

–Use a do-while loop to prompt for and read the Strings (linesof 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 stepthrough the ArrayList of

Strings and concatenate a number to the beginning of each lineeg:

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 forpoints 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 oftext)
from the user at the command line until they enter a sentinelvalue.
- Use a for loop to step through the ArrayList and concatenate aline 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 toenter?\");
  
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);
}
}
  
}

Answer & Explanation Solved by verified expert
3.6 Ratings (374 Votes)
codeimport javautilpublic class Main public static void mainString args Scanner scnr new ScannerSysteminSystemoutprintlnHow many lines of text do you want toenterchar by to continue do while    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students