File Processing- Command Line Arguments
Here is the problem I am trying to solve:
Write a program that determines and displays number of lines in a file that you specify on the command line. Example run:
java YourClass test.txt
12 lines in test.txt
I would post "Here is what I know so far", but I literally have no idea how to begin. Do I need to import the IO files for File, PrintWriter classes? Any help would be greatly appreciated.
Code :
import java.util.*;
import java.io.*;
class YourClass
{
public static void main (String[] args) throws IOException
{
DataInputStream binIn = new DataInputStream(new FileInputStream(args[0]));
int count = 0;
}
}
Re: File Processing- Command Line Arguments
Do you have to use DataInputStream/FileInputStream? The Scanner class will suffice for your purpose Scanner Class - API
Write down the steps you think your program would have to take to achieve your goal.
Step 1. - Open file
Step 2. - While lines exist, read line, increment counter
Step 4. - Close File
Step 5. - Output result
Re: File Processing- Command Line Arguments
Thank you. Still trying to figure it out.
to make a new file I would use
Scanner sc = new Scanner(new File("myNumbers")); , but I thought I am supposed to specify the file in the command line?
Re: File Processing- Command Line Arguments
Excellent.
You can still use your command line argument. How did you do it in your original code? What datatype does the File constructor take? What datatype is the command line arg?
Re: File Processing- Command Line Arguments
InputStream? Im reading through the whole Scanner class now
Re: File Processing- Command Line Arguments
Not InputStream itself, what did you pass to it rather?
What does File take in the contructor? Read here File Class - Constructor
What is args[0]??
Re: File Processing- Command Line Arguments
Quote:
Originally Posted by
Kewish
Not InputStream itself, what did you pass to it rather?
What does File take in the contructor? Read here
File Class - Constructor
What is args[0]??
File takes a string in the constructor?
the text file is passed as args[0]
Re: File Processing- Command Line Arguments
An array args index 0? Sorry. I am just a beginner
args[0] is what you enter on the command line?
Re: File Processing- Command Line Arguments
Code :
Scanner sc = new Scanner(new File("test.txt"));
DataInputStream binIn = new DataInputStream(new FileInputStream(args[0]));
File object = new File(args[0]);
Re: File Processing- Command Line Arguments
Create the File Object first. Then the Scanner with your newly created File object. Forget about DataInputStream. You will be using the Scanner.
--- Update ---
Here's a post from our very own JavaPF. Funnily enough the first hit from Google.
http://www.javaprogrammingforums.com...ner-class.html
Re: File Processing- Command Line Arguments
Thank you.. I guess I was searching incorrectly. And I really thought I needed DataInputStream no matter what
Anyway, I created a data.txt so that code would run, and nothing happened when I ran the program
Re: File Processing- Command Line Arguments
Post your completed code along with any exceptions you get in the console or other output.
Re: File Processing- Command Line Arguments
Code :
import java.io.*;
import java.util.Scanner;
public class Chapter13 {
public static void main(String[] args) {
File object = new File(args[0]);
int count = 0;
try {
Scanner sc = new Scanner(object);
while (sc.hasNextLine()) {
String line = sc.nextLine();
System.out.println(line);
count++;
System.out.println(count + " lines in " + object);
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Errors:
ArrayOutOfBoundsException on line 9
edit: Okay, solved that problem by using a different IDE.
Now here's what I enter into the console and my output (this is reading from a document with only 5 lines..);
edit: think it works now.
Re: File Processing- Command Line Arguments
I can't thank you enough Kewish. I need to start using the oracle docs more. VERY Helpful.
Final answer:
Code :
import java.io.*;
import java.util.Scanner;
public class Chapter13 {
public static void main(String[] args) {
File object = new File(args[0]);
int count = 0;
try {
Scanner sc = new Scanner(object);
while (sc.hasNextLine()) {
String line = sc.nextLine();
count++;
}
System.out.println(count + " lines in " + object);
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Run:
javac Chapter13.java
java Chapter13 sample.txt
Output:
5 lines in sample.txt