Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: File Processing- Command Line Arguments

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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.


    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;
     
     
     
            }
     
    }


  2. #2
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default 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

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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?

  4. #4
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default 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?

  5. #5
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: File Processing- Command Line Arguments

    InputStream? Im reading through the whole Scanner class now

  6. #6
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default 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]??

  7. #7
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: File Processing- Command Line Arguments

    Quote Originally Posted by Kewish View Post
    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]

  8. #8
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Yep and what is args [0]???

  9. #9
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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?

  10. #10
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    args is the array of command line arguments.

    args [0] is a value from the array. And the datatype is a String.

    Can you constuct a File object with that information? Yep.

  11. #11
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: File Processing- Command Line Arguments

    Scanner sc = new Scanner(new File("test.txt"));
    DataInputStream binIn = new DataInputStream(new FileInputStream(args[0]));
    File object = new File(args[0]);

  12. #12
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default 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

  13. #13
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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

  14. #14
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default Re: File Processing- Command Line Arguments

    Post your completed code along with any exceptions you get in the console or other output.

  15. The Following User Says Thank You to Kewish For This Useful Post:

    TSSF44 (November 21st, 2013)

  16. #15
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: File Processing- Command Line Arguments

    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.

  17. #16
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    Do you what whitespace in your text file?

    I.E. blank lines

  18. #17
    Member
    Join Date
    Oct 2013
    Posts
    31
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default 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:

     
    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

  19. #18
    Member Kewish's Avatar
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    116
    Thanks
    10
    Thanked 17 Times in 14 Posts

    Default

    You are welcome and yes they are a vital source of information.

  20. The Following User Says Thank You to Kewish For This Useful Post:

    ChristopherLowe (November 22nd, 2013)

Similar Threads

  1. Help understanding Command-Line Arguments
    By Truck35 in forum Collections and Generics
    Replies: 1
    Last Post: December 13th, 2012, 06:16 PM
  2. How to check for shortage of command line arguments
    By IHeartProgramming in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 16th, 2012, 12:00 AM
  3. command line arguments
    By rizla in forum Member Introductions
    Replies: 3
    Last Post: December 12th, 2010, 11:14 PM
  4. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java JDK & IDE Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM
  5. How to Send command line arguments in Eclipse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 23rd, 2009, 11:37 AM