Hello redvenice. Welcome to the Java Programming Forums.
You can easily use the Scanner class to do this. I have wrote a quick example for you.
For the purpose of this example I have created a file called data.txt which contains:
hello this is a test
this is a test
this is a test program
my name is monkey
javaprogrammingforums.com
hello java
test test test test
javaprogrammingforums.com
test test test test
test test test test
test test test test
The line we are looking for in the data file is 'javaprogrammingforums.com'
The code to do this is here:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Redvenice {
/**
* JavaProgrammingForums.com
*/
// line in data file to find
public static String findMe = "javaprogrammingforums.com";
public static int count = 0;
public static void main(String[] args) {
readFile();
}
public static void readFile() {
// Location of file to read
File file = new File("data.txt");
try {
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
// System.out.println(line);
if (line.equals(findMe)) {
// increase number of times found
count++;
// update jtextfields here
System.out.println("FOUND - " + line);
}
}
System.out.println(findMe + " found " + count + " times");
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
The output is this:
FOUND - javaprogrammingforums.com
FOUND - javaprogrammingforums.com
javaprogrammingforums.com found 2 times
I hope this helps solve your issues