Results 1 to 6 of 6
- 04-01-2009, 02:48 PM #1
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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:-
- 04-01-2009, 03:04 PM #2
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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.
- 04-01-2009, 03:18 PM #3
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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:-
- 04-01-2009, 03:21 PM #4
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
So you want to get the file path from the user. Do you know how to read the command line parameters? System.in?
- 04-01-2009, 03:33 PM #5
Member
- Join Date
- Mar 2009
- Posts
- 18
- Rep Power
- 0
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:-
- 04-02-2009, 05:23 AM #6
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,374
- Blog Entries
- 1
- Rep Power
- 18
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
-
Getting access denied error while importing file using input type="file" with IE7
By sarang1 in forum Advanced JavaReplies: 6Last Post: 02-10-2011, 09:55 AM -
trouble with Scanner(new File("input"));
By ronyosi in forum New To JavaReplies: 9Last Post: 10-27-2010, 11:34 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
replacing last comma in a string with the word "or"
By wprjr in forum New To JavaReplies: 1Last Post: 05-07-2008, 01:19 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks