Results 1 to 6 of 6
  1. #1
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Question Count lines cointaining "word" in input file

    I am trying to work out a problem involving finding the number of lines containing the word "hello" in an input file.

    I have got the program to read the file from the correct place and the this count produces the correct number so long as hello features in each line which is wrong.

    how would i go about producing the program so count produces the number of lines featuring the word "hello"

    Java Code:
    package assignment3;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class FindInFile {
    
       public static void main (String[] args) throws IOException
       {
          String line;
          Scanner fileScan, lineScan;
          int thisCount = 0;
    
          fileScan = new Scanner (new File("C:/urls.inp.txt"));
    
          // Read and process each line of the file
          while (fileScan.hasNext())
          {
             line = fileScan.nextLine();
             System.out.println ("Line: " + line);
    
             lineScan = new Scanner (line);
             lineScan.useDelimiter("hello");
    
             while (lineScan.hasNext())
                System.out.println ("     " + lineScan.next());
                thisCount++;
    
          }
          System.out.println("number of lines containing \"hello\" = " + thisCount);
       }
    }
    -Long time no c-
    -:eek:-

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

    Default

    For the first sprint, can you read each line of the file?

    Then try to search the text on that line, if you found only increase the count.

  3. #3
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Yes it can read each line and it now counts correctly the number of lines containing hello. The amended code is bellow:

    I forgot to ask the user for the file location.

    How do i make it so that the user chooses the input file location?

    Java Code:
    package assignment3;
    
    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;
    
    public class FindInFile {
    
        public static void main(String[] args) throws IOException {
            String line;
            Scanner fileScan, lineScan;
            int thisCount = 0;
    
            System.out.println("Enter location of input file:");
    
            fileScan = new Scanner(new File("F:/UWE/Program Development/urls.inp.txt"));
    
            // Read and process each line of the file
            while (fileScan.hasNext()) {
                line = fileScan.nextLine();
    
                lineScan = new Scanner(line);
    
                while (lineScan.hasNext()) {
                    lineScan.next();
                }
                if (line.contains("hello")) {
                    thisCount++;
                }
    
            }
            System.out.println("number of lines containing \"hello\" = " + thisCount);
        }
    }
    -Long time no c-
    -:eek:-

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

    Default

    So you want to get the file path from the user. Do you know how to read the command line parameters? System.in?

  5. #5
    gwithey is offline Member
    Join Date
    Mar 2009
    Posts
    18
    Rep Power
    0

    Default

    Yes i managed to use a scanner and got this code to work don't know if i have too many scanners but it has worked fine on several tests i have run on different input files.

    It has resolved my problem so thank you for the help

    [CODE]package assignment3;

    import java.io.File;
    import java.io.IOException;
    import java.util.Scanner;

    public class FindInFile {

    public static void main(String[] args) throws IOException {
    String line, userFileName;
    Scanner fileScan, lineScan;
    int thisCount = 0;
    Scanner scan = new Scanner(System.in);

    System.out.println("Enter location of input file:");
    userFileName = scan.nextLine();
    fileScan = new Scanner(new File(userFileName));

    // Read and process each line of the file
    while (fileScan.hasNext()) {
    line = fileScan.nextLine();
    lineScan = new Scanner(line);

    while (lineScan.hasNext()) {
    lineScan.next();
    }
    if (line.contains("hello")) {
    thisCount++;
    }//if
    }//while
    System.out.println("Number of lines containing \"hello\" = " + thisCount);
    }//main
    }//class/CODE]
    -Long time no c-
    -:eek:-

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

    Default

    You can read a text file using FileReader as well, without using a scanner.

    Java Code:
    BufferedReader input =  new BufferedReader(new FileReader(file_path));

Similar Threads

  1. Replies: 6
    Last Post: 02-10-2011, 09:55 AM
  2. trouble with Scanner(new File("input"));
    By ronyosi in forum New To Java
    Replies: 9
    Last Post: 10-27-2010, 11:34 PM
  3. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 07:35 AM
  5. Replies: 1
    Last Post: 05-07-2008, 01:19 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
  •