the counter does not working
this code i have been doing suppose to add a counter everytime the code found a term in a file. The counter represents the number of documents containing the term.
System.out.println("Please enter the required word :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan.nextLine();
String[] array2 = word2.split(" ");
for (int b = 0; b < array.length; b++) {
for (int i = 0; i < filename; i++) {
try {
BufferedReader in = new BufferedReader(new FileReader(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc"
+ i + ".txt"));
int numDoc = 0;
int numofDoc = 0;
Scanner s2 = new Scanner(in);
{
while (s2.hasNext()) {
if (s2.next().equals(word2))
numDoc++;
}
开发者_StackOverflow社区 }
if (numDoc > 0)
numofDoc++;
System.out.println("File containing the term is "
+ numofDoc);
} catch (IOException e) {
System.out.println("File not found.");
}
The output is :
Please enter the required word :
the
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
File not found
File containing the term is 1
File containing the term is 1
File containing the term is 1
File containing the term is 1
I would like the output to display the number of file containing the term is 10.
Mind to point out my mistake ? thanks..
- Indent your code properly (under Eclipse, CTRL + SHIFT + F will do it for you)
- Give sensible and explicit names to your variables. numDoc and numOfDoc are too close to avoid mistakes
- You are outputing the counter in the inner loop, try to get your
System.out.println("File containing the term is " + numofDoc);
out of the secondfor
loop (this can easily be spotted if you indent your code properly). Also check that you are outputting the right variable.
Now that you print the result in the proper place, int numofDoc = 0;
shall also be outside the second for
loop.
Additionally, you are using String.equals
to check if the current line of the file contains the required text. Maybe you want to look for the documentation of String.contains
I guess that numDoc
represents the number of occurences in the file and numofDoc
reprents the number of files.
The problem is that the variable int numofDoc = 0
is set in the for loop. So for every new file the counter is reset.
Set int numDoc = 0; before the two loops.
So you're setting back the value to 0 every time the loop is executed.
declare int numDoc = 0; int numofDoc = 0;
outside for loop.
whenever executing to for loop, they initialized & then incremented to 1. That's why you getting all time 1.
I think you want to do this
public static void main(String[] args)
{
System.out.println("Please enter the required word :");
Scanner scan2 = new Scanner(System.in);
String word2 = scan.nextLine();
String[] array2 = word2.split(" ");
for ( int b = 0; b < array.length; b++ )
{
**//Declare before the loop**
int numofDoc = 0;
for ( int i = 0; i < filename; i++ )
{
try
{
BufferedReader in = new BufferedReader(new FileReader(
"C:\\Users\\user\\fypworkspace\\TextRenderer\\abc" + i + ".txt"));
int matchedWord = 0;
Scanner s2 = new Scanner(in);
{
while ( s2.hasNext() )
{
if ( s2.next().equals(word2) )
matchedWord++;
}
}
if ( matchedWord > 0 )
numofDoc++;
System.out.println("File containing the term is " + numofDoc);
}
catch ( IOException e )
{
System.out.println("File not found.");
}
}
}
}
精彩评论