In: Computer Science
Create a Netbeans project called LineNumbers
The 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
Code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
class mullines {
    // Read multi-line input from console in Java 7 by using BufferedReader class
    public static void main(String[] args) {
        //declare and initialize n and i to 0
        int i = 0,n = 0;
        //ask user to enter number of lines
        System.out.println("Enter no.of lines of text");
        Scanner sc = new Scanner(System.in);
        n = sc.nextInt();
        //declarae a string array with n as size
        String s[] = new String[n];
        //Using InputStreamReader and BufferedReader class create object and input text lines
        try (InputStreamReader in = new InputStreamReader(System.in);
             BufferedReader b = new BufferedReader(in)) {
            //input text lines until the n lines are inputted
            while(i!=n) {
                s[i] = b.readLine();
                i++;
            }
        }
        //catch block catches any exception occurs in try
        catch (Exception e) {
        }
        //using for loop concat the line number before text.
        //after line number there should be a space so we should concat a space after line number
        for(i =0; i<n;i++)
        {
            s[i] = (i+1)+" "+ s[i];
        }
        //Using for each loop print the lines
        for(String x: s)
            System.out.println(x);
    }
}
Sample O/P1:
Enter no.of lines of
text
2
This is
The first day
1 This is
2 The first day
Sample O/P2:
Enter no.of lines of
text
5
Hello world.
I am Steve Jack
I am new to this place
could you please help me
to go there
1 Hello world.
2 I am Steve Jack
3 I am new to this place
4 could you please help me
5 to go there
Code Screenshot:


Sample O/P1 screenshot:

Sample O/P2 screenshot:

(If you still have any doubts regarding this answer please comment. I will definitely help)