Display only found results
A follow up to my Item Scanner, a java tool that scans through a directory of *.txt files.
Thank you all for the help! However now I have a different problem.
When I get the results it reads strings I do not want it to read. How can I block out these strings I do not want it reading?
This is an example what I mean:
Enter item to find: 1112
Found 125000000
[BANK]
[FRIENDS]
[IGNORES]
[EOF]
time(s) in kyle.txt!
Found 1000000
[FRIENDS]
[IGNORES]
[EOF]
time(s) in kylea.txt!
Press any key to continue . . .
I want it to display the results like this:
Enter item to find: 1112
Found 125000000 time(s) in kyle.txt!
Found 1000000 time(s) in kylea.txt!
Press any key to continue . . .
How would I do this? I tried creating an array list to block the other results I do not need, but I can't get it to work either.
Thank you for the help!
Also, here is my code:
import java.io.*;
import java.util.*;
public class ItemScanner {
public static void main(String args[]) {
System.out.print("Enter item to find: ");
Scanner sc = new Scanner(System.in);
find(sc.nextLine());
}
public static void find(String delim) {
File dir = new File("accounts");
if (dir.exists()) {
String read;
try {
File files[] = dir.listFiles();
for (int i = 0; i < files.length; i++) {
File loaded = files[i];
if (loaded.getName().endsWith(".txt")) {
BufferedReader in = new BufferedReader(new FileReader(loaded));
StringBuffer load = new StringBuffer();
while ((read = in.readLine()) != null) {
load.append(read + "\n");
}
String delimiter[] = new String(load).split(delim);
if(delimiter.length > 1) {
System.out.println("Found " + (delimiter[1]) + " time(s) in " + loaded.getName() + "!");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
System.out.println("error: dir wasn't found!");
}
}
}
Also, an example of an account it scans through, here is an account:
[ACCOUNT]
character-username = kylea
character-password =
[CHARACTER]
character-height = 0
character-isactive = 1
character-messages = 0
character-lastconnection =
character-lastlogin = 2009/11/27
character-energy = 100
[EQUIPMENT]
character-equip = 0 4724 0
character-equip = 1 1052 0
character-equip = 2 6585 0
character-equip = 3 4151 0
character-equip = 4 4720 0
character-equip = 5 1215 0
character-equip = 6 -1 0
character-equip = 7 4722 0
character-equip = 8 -1 0
character-equip = 9 775 0
character-equip = 10 1837 0
character-equip = 11 -1 0
character-equip = 12 6735 0
character-equip = 13 -1 0
[LOOK]
character-look = 0 1
[SKILLS]
character-sk开发者_开发知识库ill = 0 18 445633
character-skill = 1 15 440000
character-skill = 2 15 440000
character-skill = 3 199 1402300000
character-skill = 4 22 0
character-skill = 5 1 0
character-skill = 6 1 0
character-skill = 7 1 0
character-skill = 8 1 0
character-skill = 9 1 0
character-skill = 10 1 0
character-skill = 11 1 0
character-skill = 12 1 0
character-skill = 13 1 0
character-skill = 14 1 0
character-skill = 15 1 0
character-skill = 16 1 0
character-skill = 17 1 0
character-skill = 18 1 0
character-skill = 19 1 0
character-skill = 20 1 0
character-skill = 21 1 0
character-skill = 22 1 0
character-skill = 23 1 0
character-skill = 24 1 0
[ITEMS]
character-item = 0 6570 11
character-item = 1 666570 0525
character-item = 2 1165701 55
character-item = 3 55 66
character-item = 4 963 51
character-item = 5 961 55
[BANK]
character-bank = 0 996 1000000
[FRIENDS]
[IGNORES]
[EOF]
What you are doing in this code is reading the contents of .txt files in a string buffer and splitting that file contents on a delimeter, in your example "1112" and printing the remainder of that file.
so if your file contains "aaaa11121112uh oh" it would print "1112uh oh" ? It is not clear to me what you try to accomplish.
A quick fix to only showing the number would be to use:
Long.valueOf(delimiter[1])
to parse only the number part of the remainder and print that. I have the feeling that this would not solve the underlying problem, so maybe you can elaborate on what you are trying to achieve here?
Edit
After you updated your question with an example of a data file, (which looks very much like an .ini file :-)) I think you would do good by reading through the javadoc for Properties class, in particular the load() and store() methods, storing your character data in a Properties
collection.
You appear to have mixed up some example code. You seem from the question and main() to be looking for a particular item, but in the code, you're using the passed-in argument as a delimiter to split the line you read, and counting the number of words.
To do what your question asks, you'd need logic in which you split the read-in line, then iterate through it, comparing against the item to find.
精彩评论