Skip to content

Instantly share code, notes, and snippets.

@1234
Created August 26, 2014 18:16
Show Gist options
  • Save 1234/e02266395377fdddf713 to your computer and use it in GitHub Desktop.
Save 1234/e02266395377fdddf713 to your computer and use it in GitHub Desktop.
File TestFile.txt, uploaded by WinGists.
TestFileContent
@catmommyangie
Copy link

import java.util.Scanner;
public class LineNumbers{
public static void main(String args[]) throws IOException{
Scanner input = new Scanner(System.in);

    System.out.println("Enter file name: ");
    String fileName = input.nextLine();
    
    File file = new File(fileName + ".txt");
    
    if(!file.exists()){
        System.exit(0);
        
        Scanner inputFile = new Scanner(file);
        
        String line;
        
        int counter = 0;
        
        while(inputFile.hasNext()){
            counter += 1;
            line = inputFile.nextLine();
            System.out.println(counter + ": " + line);
        }
        inputFile.close();
    }

}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment