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.
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 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 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 asks the user for a file name. The file contains a series...
Write a program that asks the user for a file name. The file contains a series of scores(integers), each written on a separate line. The program should read the contents of the file into an array and then display the following content: 1) The scores in rows of 10 scores and in sorted in descending order. 2) The lowest score in the array 3) The highest score in the array 4) The total number of scores in the array 5)...
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
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 Explain step by step
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT