Results 1 to 5 of 5
  1. #1
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default Command line argument help...

    I need help with specifying multilple file names on the command line to proccess passages in each of them in order All in one run... Each passage(file) has to be processed separately with the output for the different tasked independent of the other passages.. Meaning, process the first passage and produce the required output, then process the second passage and so on..


    For example, the two files that i wanna read in on the command line is called p2p.txt and google.txt

    How i would read in only one is something like: java project p2p.txt

    How do i process both?


    Java Code:
          // Check for existence of the file
    
          File infile = new File(args[0]);
    
    
          if(!infile.exists())
          {
    
             System.out.println("The file" +args[0]+ "does not exists.");
             System.out.println("Quitting now. Please try again.");
             System.exit(1);
          }



    Also, how do lose the precision on something like:
    Java Code:
    (prepositions_count[i]/(double)num_words)*100);

    or

    Java Code:
    (biagrams_count[i]/(double)num_biagrams)*100);


    Where something like 5.400981996726677 becomes 5.4?
    Last edited by arson09; 05-06-2010 at 05:34 AM.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    java project p2p.txt google.txt
    Java Code:
    File infile1 = new File(args[0]);
    File infile2 = new File(args[1]);
    For number formatting look at DecimalFormat.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    And it's better to validate them in a loop, hence easily you can capture all the arguments passing in.

  4. #4
    arson09 is offline Member
    Join Date
    Mar 2010
    Posts
    21
    Rep Power
    0

    Default

    Quote Originally Posted by PhHein View Post
    java project p2p.txt google.txt
    Java Code:
    File infile1 = new File(args[0]);
    File infile2 = new File(args[1]);
    For number formatting look at DecimalFormat.


    That doesn't work..... Maybe if i give an example to my whole code, it'll be more clear with what i'm talking about:


    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class StringProccessing
    {
       public static void main(String[] args)
       throws java.io.IOException
       {
    
       // Declare Variables
    
          int i;
          int j= 0;
          int num_lines = 0;
          int num_words=0;
          String infilename;
          String str [] = new String [1000];
          String [] words = new String [10000];
          String longest_word = "";
    
        
          // Command line argument
    
          File infile = new File(args[0]);
    
    
          if(!infile.exists())
          {
    
             System.out.println("The file" +args[0]+ "does not exists.");
             System.out.println("Quitting now. Please try again.");
             System.exit(1);
          }
    
    
        //Open the file for reading
    
         Scanner fsc = new Scanner(infile);
    
    
        //Read the data in the file line by line
         System.out.println("\nPrint the passage:");
         System.out.println("-----------------");
        while (fsc.hasNext() )
        {
            str[num_lines]= fsc.nextLine();
            System.out.println(str[num_lines]);
            num_lines ++;
        }
    
         // Capitalize first character of each word and  Count the total number of words in passage
         System.out.println("\nCapitalize first character of each word and  Count the total number of words in passage:");
         System.out.println("----------------------------------------------------------------------------------------");
         for(i=0; i<num_lines; i++)
         {
             Scanner wordScanner = new Scanner(str[i]);
             while (wordScanner.hasNext() )
             {
                words[num_words]= wordScanner.next();
                Character.toUpperCase(words[num_words].charAt(0));
                System.out.print(Character.toUpperCase(words[num_words].charAt(0)) + words[num_words].substring(1) + " ");
                num_words ++;
             }
             System.out.println();
         }
    
          System.out.println("\nThe number of words in the passage: " +  num_words);
    
          int word_length=0;
          int longest=0;
    
          for(i=0; i<num_words; i++)
          {
              if(words[i].trim().endsWith("s"))
                  word_length = words[i].trim().length() -1;
              else
                  word_length = words[i].trim().length();
    
              if(word_length > longest)
              {
                  longest = word_length;
                  longest_word = words[i].trim();
              }
          }
    
          System.out.println("\nThe longest word in the passage: " + longest_word);
          System.out.println("The length of the longest word: " + longest);
    
    
        // Change all occurrences of the word "The" to the word "La" and the word "the" to the word "la"
         System.out.println("\nChange all occurrences of the word The to the word La and the word the to the word la:");
         System.out.println("-------------------------------------------------------------------------------------");
         for(i=0; i<num_lines; i++)
         {
             Scanner wordScanner = new Scanner(str[i]);
             while (wordScanner.hasNext() )
             {
                String word = wordScanner.next();
                if(word.equals("The"))
                    word = "La";
                if(word.equals("the"))
                    word = "la";
                System.out.print(word + " ");
             }
             System.out.println();
         }
    
    
          //Print frequency table for prepositions "of,""to,""in,""for,""whith,"" and "one."
          String prepositions [] = {"of", "to", "in", "for", "With", "on"};
          int prepositions_count[] = new int[6];
          int num_prepositions=0;
          for(i=0; i<num_words; i++)
          {
              for(j=0; j<prepositions.length; j++)
              {
                  if(words[i].equalsIgnoreCase(prepositions[j]))
                  {
                      prepositions_count[j]++;
                      num_prepositions++;
                  }
              }
    
          }
    
          System.out.println("\nPreposition| Frequency | % ");
          System.out.println("----------------------------");
          for(i=0; i<prepositions.length; i++)
          {
              System.out.println(prepositions[i] + " | " + prepositions_count[i] + " | " + (prepositions_count[i]/(double)num_words)*100);
          }
          System.out.println("----------------------------");
          System.out.println("Total | " + num_prepositions + " | " + (num_prepositions/(double)num_words)*100);
    
       }
    
    }



    Now When i enter java p2p.txt goolge.txt, it would print out the statements in my code, in the same order for both files and so forth... With the above code that you suggested, it still only gives me the information for p2p.txt.

  5. #5
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You're going to need to do some refactoring, which frankly you should have done before now anyway.
    Quote Originally Posted by arson09 View Post
    Java Code:
    import java.util.*;
    import java.io.*;
    
    public class StringProccessing
    {
       public static void main(String[] args)
       throws java.io.IOException
       {
            ...
          // Command line arguments
          for (String arg : args) {
                File infile = new File(arg);
                if (!infile.exists) {
                    System.err.println("File '" + arg + "' does not exist.");
                    System.err.println("Continuing with next file.");
                    continue;
                }
                processFile(infile);
          }
       }
    
       public static void processFile(File infile) {
        //Open the file for reading
    
         Scanner fsc = new Scanner(infile);
    
    
        //Read the data in the file line by line
         System.out.println("\nPrint the passage:");
          ...
       }
    
    }
    Your processFile() method should itself be decomposed into several more methods. Good luck!

    -Gary-
    Last edited by gcalvin; 05-06-2010 at 05:52 PM.

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. command line arguments
    By evermore in forum New To Java
    Replies: 5
    Last Post: 03-28-2010, 05:08 AM
  3. arguments in command-line
    By girlet18 in forum New To Java
    Replies: 2
    Last Post: 01-21-2010, 02:05 PM
  4. Command Line Argument
    By right2001 in forum New To Java
    Replies: 6
    Last Post: 02-17-2009, 02:08 PM
  5. Unable to execute command line command in java
    By LordSM in forum New To Java
    Replies: 1
    Last Post: 08-08-2007, 12:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •