In: Computer Science
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.
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>