Question

In: Computer Science

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

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);
}
}
  
}

Solutions

Expert Solution

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:


Related Solutions

Modify the program 5-13 from page 279 such that will also compute the class average. This...
Modify the program 5-13 from page 279 such that will also compute the class average. This class average is in addition to each individual student score average. To accomplish this additional requirement, you should do the following: 1. Add two more variables of type double: one for accumulating student averages, and one to hold the class average. Don't forget, accumulator variable should be initialized to 0.0. 2. Immediately after computing individual student average, add a statement that will accumulate the...
You are to modify your payroll program from the last assignment to take the new copy...
You are to modify your payroll program from the last assignment to take the new copy of the payroll file called DeweyCheatemAndHow.txt which has the fields separated by a comma to use as input to your program and produce a file that lists all the salaried employee and their gross pay, then each hourly employee and their gross pay and finally each piece rate employee and their gross pay . You are to process all the entries provided in the...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program...
CS 1102 Unit 5 – Programming AssignmentIn this assignment, you will again modify your Quiz program from the previous assignment. You will create an abstract class called "Question", modify "MultipleChoiceQuestion" to inherit from it, and add a new subclass of "Question" called "TrueFalseQuestion". This assignment will again involve cutting and pasting from existing classes. Because you are learning new features each week, you are retroactively applying those new features. In a typical programming project, you would start with the full...
Modify the code below to implement the program that will sum up 1000 numbers using 5...
Modify the code below to implement the program that will sum up 1000 numbers using 5 threads. 1st thread will sum up numbers from 1-200 2nd thread will sum up numbers from 201 - 400 ... 5th thread will sum up numbers from 801 - 1000 Make main thread wait for other threads to finish execution and sum up all the results. Display the total to the user. #include <pthread.h> #include <stdio.h> #include <stdlib.h> #define N_OF_THREADS 4 void * print(void...
Your task is to modify the program from the Java Arrays programming assignment to use text...
Your task is to modify the program from the Java Arrays programming assignment to use text files for input and output. I suggest you save acopy of the original before modifying the software. Your modified program should: contain a for loop to read the five test score into the array from a text data file. You will need to create and save a data file for the program to use. It should have one test score on each line of...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when...
"4. (Modify) Modify Program 7.14 so that the user inputs the initial set of numbers when the program runs. Have the program request the number of initial numbers to be entered." //C++ Program 7.14 as follows #include #include #include #include using namespace std; int main() { const int NUMELS = 4; int n[] = {136, 122, 109, 146}; int i; vector partnums(n, n + NUMELS); cout << "\nThe vector initially has the size of " << int(partnums.size()) << ",\n and...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file...
Get the file “HW4Part4b.java” from the repository. Modify the program class to match the new file name. Complete it by writing a recursive static int function named recur that is defined as follows: if i ≤ 0 or j ≤ 0, recur(i, j) = 0. if i = j, recur(i, j) = i. if i > j, recur(i, j) = j. In all other cases, recur(i, j) = 2 · recur(i − 1, j) + recur(j − 1, i) Add...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from...
4. Modify the program geometryDemo to use the class basicGeometry and the threeSides code. Note from the comments in the main routine that it wants an input argument of 3 to solve for a triangle and 4 to solve for a square or rectangle. MODIFY the code to add another method in the 'threeSides' and 'fourSides' classes to return the perimeter. ASSUME the following for TRIANGLES. ** for AREA (1/2 * base * height) dimension1 is base, dimension2 is height...
Modify the Assignment program from Module 1 to read a user's first and last name read...
Modify the Assignment program from Module 1 to read a user's first and last name read three integer scores, calculate the total and average determine the letter grade based on the following criteria - Average 90-100 grade is A, 80-89.99 grade is B, 70-79.99 grade is C, all others F (use a nested if) Output formatted results consisting of the full name, the three scores, the total, average (to 2 decimal places), and the letter grade go to step 1...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be...
Modify the DetailedClockPane.java class in your detailed clock program, to add animation to this class. Be sure to include start() and stop() methods to start and stop the clock, respectively.Then write a program that lets the user control the clock with the start and stop buttons.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT