In: Computer Science
The JAVA program should do the following:
–Ask the user for how many lines of text they wish to enter
–Declare and initialize an array of Strings to hold the user’s input
–Use a while loop to prompt for and read the Strings (lines of
text) from the user
at the command line. Typically, this would be a 'for' loop since we
know the number of times to execute the loop based upon the number
supplied by the user, but for the purposes of this assignment use a
'while' loop to practice.
–After the Strings have been read, use a for loop to step
through the array of
Strings and concatenate a number to the beginning of each line and
store them back in to the array:
"This is" changes to "1 This is"
"The first day" changes to "2 The first day"
and so on…
–Finally, use a **for each** loop to output the Strings (numbered lines) to the command line
import java.util.Scanner;
/**
* @author
*
*/
public class LineNumbers {
/**
* @param args
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter number of lines: ");
int n=sc.nextInt();
sc.nextLine();
String arr[]=new String[n];
int index=0;
System.out.print("Enter lines: ");
while(index<n) {
arr[index]=sc.nextLine();
index++;
}
for(index=0;index<n;index++) {
arr[index]=(index+1)+": "+arr[index];
}
for(String s:arr)
System.out.println(s);
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME