Question

In: Computer Science

Create a Netbeans project called LineNumbers The program should do the following: –Ask the user for...

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

Solutions

Expert Solution

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)


Related Solutions

Create a java program that will do the following: Create a method called getInt.Allow the user...
Create a java program that will do the following: Create a method called getInt.Allow the user to enter up to 20 student names,and for each student 3 quiz scores (in the range 0-100). Once input is done, display each student’s name, their three quiz scores, and their quiz score average, one student per line. The output table does not need to line up perfectly in columns.Use dialog boxes for all input and output.Use the method to input the three scores.Parameter...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a...
2. Create a new NetBeans project called PS1Gradebook. Your program will simulate the design of a student gradebook using a two-dimensional array. It should allow the user to enter the number of students and the number of assignments they wish to enter grades for. This input should be used in defining the two-dimensional array for your program. For example, if I say I want to enter grades for 4 students and 5 assignments, your program should define a 4 X...
Create a small program that contains the following. ask the user to input their name ask...
Create a small program that contains the following. ask the user to input their name ask the user to input three numbers check if their first number is between their second and third numbers
The JAVA program should do the following: –Ask the user for how many lines of text...
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...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two...
what should this program do? Write a program (Lab8.cpp) that will ask the user for two file names. You will read the characters from each file and put them in separate queues. Then, you will read each character from each queue and compare it. If every character from both queues is the same, you will print “The files are identical.” Otherwise, you will print “The files are not identical.” Step-by-Step Instructions Create a character queue (named queue1) using the Standard...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input...
Create method addUserInput Write a method called addUserInput(). The method should ask the user to input two integers (one by one), add the two integers, and return the sum. By using java.util.Scanner to get user input; The method may not compile due to Scanner Class which need to add a "throws" statement onto the end of the method header because some lines may throw exceptions Refer to the Java API documentation on Scanner to figure out which specific Exception should...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the...
Create a project that gets input from the user. ( Name the project main.cpp) Ask the user for a value for each side of the triangle. Comment your code throughout. Include your name in the comments. Submit the plan-Psuedo code (include Flowchart, IPO Chart) and the desk check with each submission of your code. Submit the plan as a pdf. Snips of your output is not a plan. Save the plan and desk check as a pdf Name the code...
Create a C++ program that will ask the user for how many test scores will be...
Create a C++ program that will ask the user for how many test scores will be entered. Setup a while loop with this loop iteration parameter. (no fstream) The data needs to include the student’s first name, student number test score the fields should be displayed with a total width of 15. The prompt should be printed with a header in the file explaining what each is: ex. First Name student number Test Score 1) mike 6456464   98 2) phill...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Write a Java program to do the following: Ask the user to enter 10 first names...
Write a Java program to do the following: Ask the user to enter 10 first names (one word - no hyphen or apostrophe) using the keyboard. 1) Display the list of names one per line on the Console. 2) Display the names again, after eliminating duplicates using a HashSet (Your code MUST use HashSet).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT