Searching File for a given String using Command-line arg.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • emcee
    New Member
    • Feb 2008
    • 1

    Searching File for a given String using Command-line arg.

    I'm to create a program that reads files through the command-line
    using a for loop to process each of the file
    (ie. search for string "out" in given files)
    using scanner methods such as hasNextline() and nextLine etc.

    the only thing i am having trouble with is the for loop.
    how would i do this?

    thanks in advance.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by emcee
    I'm to create a program that reads files through the command-line
    using a for loop to process each of the file
    (ie. search for string "out" in given files)
    using scanner methods such as hasNextline() and nextLine etc.

    the only thing i am having trouble with is the for loop.
    how would i do this?

    thanks in advance.
    Post what you have done so far.

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by emcee
      I'm to create a program that reads files through the command-line
      using a for loop to process each of the file
      (ie. search for string "out" in given files)
      using scanner methods such as hasNextline() and nextLine etc.

      the only thing i am having trouble with is the for loop.
      how would i do this?

      thanks in advance.


      Hi....
      what u have used is rt...
      Following code may help u...
      import java.util.Scann er;
      import java.io.*;
      class Parse
      {
      public static void main(String args[])
      {
      try{
      Scanner sc = new Scanner(new File("C:/Data/Customer.txt")) ;
      int count = 0;
      while(sc.hasNex t())
      {
      String result = (String)sc.next ();
      if(result.conta ins("for")){
      count = count+1;
      }
      }
      }catch(Exceptio n e){
      e.printStackTra ce();
      }
      }
      }


      -Hamsa

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        What is that command line supposed to contain? The names of the files to test?

        kind regards,

        Jos

        Comment

        • gaya3
          New Member
          • Aug 2007
          • 184

          #5
          Originally posted by JosAH
          What is that command line supposed to contain? The names of the files to test?

          kind regards,

          Jos
          yes..

          import java.util.Scann er;
          import java.io.*;
          class Parse
          {
          public static void main(String args[])
          {
          try{
          Scanner sc = new Scanner(new File(args[0]));
          int count = 0;
          while(sc.hasNex t())
          {
          String result = (String)sc.next ();
          if(result.conta ins("for")){
          count = count+1;
          }
          }
          System.out.prin tln(count);
          }catch(Exceptio n e){
          e.printStackTra ce();
          }
          }
          }


          where args[0] is here Filename..

          -Thanks & Regards
          Hamsa

          Comment

          • JosAH
            Recognized Expert MVP
            • Mar 2007
            • 11453

            #6
            Originally posted by gaya3
            yes..

            import java.util.Scann er;
            import java.io.*;
            class Parse
            {
            public static void main(String args[])
            {
            try{
            Scanner sc = new Scanner(new File(args[0]));
            int count = 0;
            while(sc.hasNex t())
            {
            String result = (String)sc.next ();
            if(result.conta ins("for")){
            count = count+1;
            }
            }
            System.out.prin tln(count);
            }catch(Exceptio n e){
            e.printStackTra ce();
            }
            }
            }


            where args[0] is here Filename..

            -Thanks & Regards
            Hamsa
            You should loop through all the file names args[i] and process them. btw what do
            you do with 'words' such as "forkformfo rum" when you search for the word "for"?

            kind regards,

            Jos

            Comment

            • gaya3
              New Member
              • Aug 2007
              • 184

              #7
              Originally posted by JosAH
              You should loop through all the file names args[i] and process them. btw what do
              you do with 'words' such as "forkformfo rum" when you search for the word "for"?

              kind regards,

              Jos

              Hi,
              Thank u for correcting my code..
              I have altered n now it works fine..

              import java.util.Scann er;
              import java.io.*;
              class Parse{
              public static void main(String args[]){
              try{
              Scanner sc = new Scanner(new File(args[0]));
              int count = 0;
              while(sc.hasNex t()){
              String result = (String)sc.next ();
              if(result.conta ins("for")){
              for(int i=0;i<result.le ngth();){
              int j = i+3;
              if(j<=result.le ngth()){
              if("for".equals (result.substri ng(i,i+3)))
              count++;
              }
              i=i+3;
              }
              }
              }
              System.out.prin tln("count::::: ::::"+count);
              }catch(Exceptio n e){
              e.printStackTra ce();
              }
              }
              }

              -Thanks 7 Regards,
              Hamsa

              Comment

              Working...