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
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will...
Create a program (Python) YourFirstnameLastnameA06b.py to ask the user to create a password: The user will first enter a password, then enters the same password again; If the second input is the same as first one, the user successfully creates the password. Print “Well done.”; Otherwise, the user will be directed to repeat the whole process (go to step 1.)
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...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a...
Create a class called clsTextProcessor.java. Your program should deliver the following: Asks the user for a location of a text file, with (FileNotFound) exception handling: Keep asking for a valid file name and location If an invalid file is provided, display an error message. Once provided, proceed. Opens the text file and finds the following: Minimum value Maximum value The average value of all number in the text Prints a message displaying the values in step No. 2
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first...
USING JAVA (netbeans) In your main, ask the user for an int. Do this by first printing a request for the int, then creating an int x, and using the Scanner to read an int from the user and put it in x, like this: int x = scan.nextInt(); ☑ Next read in two doubles d1 and d2 from the user. This will look a lot like what you did for the int, but the scanner reads doubles using scan.nextDouble();...
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...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask...
Ask the user for their name Create a file called "daily.dat" within the home directory Ask the user for a line of text that the system should save to the file Add to the file the line of text entered by the user Ask the user for a second line of text that the system should save to the file Append at the end of the file the line of text entered by the user Create a copy of the...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT