Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 04-01-2009, 03:48 PM
Member
 
Join Date: Mar 2009
Posts: 18
Rep Power: 0
gwithey is on a distinguished road
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"

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-
--
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 04-01-2009, 04:04 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
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.
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 04-01-2009, 04:18 PM
Member
 
Join Date: Mar 2009
Posts: 18
Rep Power: 0
gwithey is on a distinguished road
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?

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-
--
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 04-01-2009, 04:21 PM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
So you want to get the file path from the user. Do you know how to read the command line parameters? System.in?
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 04-01-2009, 04:33 PM
Member
 
Join Date: Mar 2009
Posts: 18
Rep Power: 0
gwithey is on a distinguished road
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-
--
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 04-02-2009, 06:23 AM
Eranga's Avatar
Moderator
 
Join Date: Jul 2007
Location: Colombo, Sri Lanka
Posts: 7,523
Rep Power: 11
Eranga has a spectacular aura aboutEranga has a spectacular aura about
Send a message via Yahoo to Eranga
Default
You can read a text file using FileReader as well, without using a scanner.

Code:
BufferedReader input =  new BufferedReader(new FileReader(file_path));
__________________
Use an appropriate Subject. "Help, urgent!" isn't one.
Someone helped you? their helpful post.
Help:Forums FAQ|How To Ask Questions The Smart WayResources:The Java Tutorials|Glossary for Java|NetBeans IDE|Sun DownloadsWeb:WritOnceTips:Is your IDE the best?|Which Application Server?
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting access denied error while importing file using input type="file" with IE7 sarang1 Advanced Java 5 03-08-2009 02:50 AM
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan") soc86 New To Java 2 01-24-2009 07:56 PM
the dollar sign "$", prints like any other normal char in java like "a" or "*" ? lse123 New To Java 1 10-20-2008 08:35 AM
trouble with Scanner(new File("input")); ronyosi New To Java 7 06-16-2008 12:33 PM
replacing last comma in a string with the word "or" wprjr New To Java 1 05-07-2008 02:19 AM


All times are GMT +2. The time now is 10:28 AM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org