-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineNumberDisplay.java
46 lines (38 loc) · 1.3 KB
/
LineNumberDisplay.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* Write a program that asks the user to enter today’s
* sales for five stores. The program should display a
* bar chart comparing each store’s sales, where each
* asterisk (*) represents $100 in sales.
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class LineNumberDisplay {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Ask the user for the file name
System.out.print("Enter the file name: ");
String fileName = scanner.nextLine();
try {
File file = new File(fileName);
Scanner fileScanner = new Scanner(file);
int lineNumber = 1;
// Read and display each line with a line number
while (fileScanner.hasNextLine()) {
String line = fileScanner.nextLine();
System.out.println(lineNumber + ": " + line);
lineNumber++;
}
fileScanner.close();
} catch (FileNotFoundException e) {
System.out.println("File not found: " + fileName);
}
scanner.close();
}
}
/**
* Explanation:
The program reads each line of the file and prints it with
a line number. The line numbers start at 1 and increase with
each line.
*/