0

How can I get this program to read in the "lab13.txt" in the command line? I've been trying to figure this out for over an hour and nothing seemed to work.

The prompt is "Write a program that determines and displays the number of lines in the file whose name you specify on the command line. Test your program with lab13.txt."

import java.util.Scanner;
import java.io.*;
class homework
{
    public static void main(String[] args) throws IOException
    {
        Scanner inFile= new Scanner(new File("lab13.txt"));
        int count=0;
        String s;
        while (inFile.hasNextLine())
        {
            s = inFile.nextLine();
            count++;
        }
        System.out.println(count + " Lines in lab13.txt");
        inFile.close();
    }
}
user2920249
  • 137
  • 4
  • 13

3 Answers3

0

If you want the user to be able to input the filename from command line or console in eclipse try using this

BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    System.out.print("Please enter filename : ");
    String filename = null;
    try {
        filename = reader.readLine();
    } catch (IOException e) {
        e.printStackTrace();
    } 

You can then plug the filename into your Scanner object

Allan Macmillan
  • 1,481
  • 3
  • 18
  • 30
0

http://docs.oracle.com/javase/tutorial/essential/environment/cmdLineArgs.html

The stuff you add to the command-line after the program name go in the args array, so:

Scanner inFile= new Scanner(new File(args[0]));
SpliFF
  • 38,186
  • 16
  • 91
  • 120
0
Try this

In your code you need to replace new File("lab13.txt") with new File(args[0])

For command line

public static void main(String[] args) {

File inFile =null;
  if (0 < args.length) {
      File inFile = new File(args[0]);
  }

    BufferedReader br = null;

    try {

        String sCurrentLine;

        br = new BufferedReader(new FileReader(inFile));

        while ((sCurrentLine = br.readLine()) != null) {
            System.out.println(sCurrentLine);
        }

    } 

    catch (IOException e) {
        e.printStackTrace();
    } 

    finally {
        try {
            if (br != null)br.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}

For a particular location

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;

public class BufferedReaderExample {

    public static void main(String[] args) {

        BufferedReader br = null;

        try {

            String sCurrentLine;

            br = new BufferedReader(new FileReader("C:\\lab13.txt"));

            while ((sCurrentLine = br.readLine()) != null) {
                System.out.println(sCurrentLine);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (br != null)br.close();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }

    }
}
Vinay Shukla
  • 1,818
  • 13
  • 41