Question

In: Computer Science

Write a program that allows the user to navigate the lines of text in a file....

Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program quits. Otherwise, the program prints the line associated with that number.

An example file and the program input and output is shown below:

example.txt

Line 1.
Line 2.
Line 3.

Enter the input file name: example.txt

The file has 3 lines.
Enter a line number [0 to quit]: 2
2 :  Line 2.
The file has 3 lines.
Enter a line number [0 to quit]: 4
ERROR: line number must be less than 3.
The file has 3 lines.
Enter a line number [0 to quit]: 0

Make sure the program gracefully handles a user entering a line number that is too high.

Grading

When you have completed your program, click the Submit button to record your score.

Solutions

Expert Solution


Java Code For Above Problem:

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;

public class FileReading {

        public static void main(String[] args) throws FileNotFoundException {

                Scanner console = new Scanner(System.in);

                // Prompt user to enter input file name
                System.out.print("Enter the input file name: ");
                String fileName = console.nextLine();

                // Create list to to store lines of text from input file
                ArrayList<String> list = new ArrayList<String>();
                // Scanner Object to read list from input file
                Scanner scanner = new Scanner(new File(fileName));

                /*
                 * Calling read list method to read data from input file and store in list
                 */

                readData(list, scanner);

                int size = list.size();// number of lines in input file

                System.out.println();
                int line;// to take line number from user

                // infinate loop
                while (true) {

                        // showing how many lines in input file
                        System.out.printf("The file has %d lines.\n", size);

                        // prompting user to enter line number
                        System.out.print("Enter a line number [0 to quit]: ");
                        line = console.nextInt();

                        // if user enter '0' stop
                        if (line == 0) {
                                break;
                        }
                        // if user entered line is higher than size of input file,display error
                        else if (line > size) {
                                System.out.println("ERROR: line number must be less than " + size + ".");
                        }
                        // Otherwise print the content of entered line
                        else {
                                System.out.println(line + ": " + list.get(line - 1));
                        }

                }

                console.close();
        }

        /**
         * 
         * @param list     :list to hold data of input file
         * @param scanner: to read the data from input file
         */
        private static void readData(ArrayList<String> list, Scanner scanner) {

                // if file contain more line
                while (scanner.hasNextLine()) {
                        // take the current line from file
                        String line = scanner.nextLine();
                        // add that line to list
                        list.add(line);
                }
        }
}

Sample Run Results:

Enter the input file name: example.txt

The file has 3 lines.
Enter a line number [0 to quit]: 2
2: Line 2.
The file has 3 lines.
Enter a line number [0 to quit]: 4
ERROR: line number must be less than 3.
The file has 3 lines.
Enter a line number [0 to quit]: 0

<example.txt>

Line 1.
Line 2.
Line 3.

Images Of Java Code For Above Problem:

Image Of Sample Run Results:

Image Of <example.txt>


Related Solutions

Write a program that allows the user to navigate the lines of text in a file....
Write a program that allows the user to navigate the lines of text in a file. The program should prompt the user for a filename and input the lines of text into a list. The program then enters a loop in which it prints the number of lines in the file and prompts the user for a line number. Actual line numbers range from 1 to the number of lines in the file. If the input is 0, the program...
Write a Java program that allows the user to specify a file name on the command...
Write a Java program that allows the user to specify a file name on the command line and prints the number of characters, words, lines, average number of words per line, and average number of characters per word in that file. If the user does not specify any file name, then prompt the user for the name.
C++ Code You will write a program to process the lines in a text file using...
C++ Code You will write a program to process the lines in a text file using a linked list and shared pointers. You will create a class “Node” with the following private data attributes: • Line – line from a file (string) • Next (shared pointer to a Node) Put your class definition in a header file and the implementation of the methods in a .cpp file. The header file will be included in your project. If you follow the...
Write a GUI-based program that allows the user to open, edit, and save text files. The...
Write a GUI-based program that allows the user to open, edit, and save text files. The GUI should include a labeled entry field for the filename and multi-line text widget for the text of the file. The user should be able to scroll through the text by manipulating a vertical scrollbar. Include command buttons labeled Open, Save, and New that allow the user to open, save and create new files. The New command should then clear the text widget and...
Write in Python and as 2 seperate programs Write a program that allows the user to...
Write in Python and as 2 seperate programs Write a program that allows the user to enter the total rainfall for each of 12 months into a LIST. The program should calculate and display the total rainfall for the year, the average monthly rainfall, and the months with the highest and lowest rainfall amounts. Data: January 7.9 inches February 10.1 inches March 3.4 inches April 6.7 inches May 8.9 inches June 9.4 inches July 5.9 inches August 4.1 inches September...
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a C++ program to create a text file. Your file should contain the following text:...
Write a C++ program to create a text file. Your file should contain the following text: Batch files are text files created by programmer. The file is written in notepad. Creating a text file and writing to it by using fstream: to write to a file, you need to open thew file as write mode. To do so, include a header filr to your program. Create an object of type fsrteam. Open the file as write mode. Reading from a...
Write a program that will ask for the user to input a filename of a text...
Write a program that will ask for the user to input a filename of a text file that contains an unknown number of integers. And also an output filename to display results. You will read all of the integers from the input file, and store them in an array. (You may need to read all the values in the file once just to get the total count) Using this array you will find the max number, min number, average value,...
Write a Java program that will test lines of text to see if they are palindromes....
Write a Java program that will test lines of text to see if they are palindromes. The program should prompt the user for the name of a file to process, then open and read the file of possible palindromes (one per line of text). The program should then print the total number of lines read, and total number of palindromes.
Write a program that runs on SPIM that allows the user to enter the number of...
Write a program that runs on SPIM that allows the user to enter the number of hours, minutes and seconds and then prints out the total time in seconds. Name the source code file “seconds.asm
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT