Repository navigation
Expand file tree
/
Copy pathLine Numbers
More file actions
67 lines (45 loc) · 1.54 KB
/
Copy pathLine Numbers
File metadata and controls
67 lines (45 loc) · 1.54 KB
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Tutor
*/
public class LineNumber {
public static void main(String[] args) throws IOException
{
String friendName;
String fileName;
//Creats Scanner Object for keyboard input
Scanner keyboard = new Scanner (System.in);
// get file name
System.out.print(" Enter the Filename :");
fileName =keyboard.nextLine();
System.out.println("");
File file = new File (fileName );
// Ensure that file exist
if(!file.exists()){
//String fileName = null;
System.out.println("The file " + fileName + " does not exist.");
System.exit(0);
}
// open file
Scanner inputFile = new Scanner (file);
// Read lines from file until there are no more left
// Initialize counter
int x = 1;
// While loop
while(inputFile.hasNext()){
// Read the next line
friendName = inputFile.nextLine();
// Display the line
System.out.println(" Line " + x + ")" + "\t\t" + friendName );
x += 1; }
inputFile.close();
}
}