Java - comparing string in ArrayList to all text in .txt file
The actual problem will be addressed a bit further down :), thanks.
I'm fairly new to Java (nearly through a 400 page book).
I'm not really that familiar with the API yet.
This is my best shot at reading a .txt file and checking if there are any of the collected data that already is stored in the .txt file. If that is the case, the data will be removed from the data collection, and the data that isn't already found in the .txt will be added.
Some variables:
public String[] names;
public int[] levels;
public int[] IDs;
public ArrayList<String&开发者_StackOverflowgt; line = new ArrayList<String>();
public ArrayList<RSNPC> monsterList = new ArrayList<RSNPC>();
public ArrayList<String> monstersToAdd = new ArrayList<String>();
The method that checks the existing .txt file:
private void checkForLine() {
try{
// Create file
File file = new File(getCacheDirectory() + "output.txt");
RandomAccessFile out = new RandomAccessFile(file, "rw");
for(int i = 0; i < file.length(); i++){
line.add(out.readLine());
}
for(String monster : monstersToAdd){
if(line.contains(monster)){
monstersToAdd.remove(monster);
}
}
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
The method that then finally saves the information that the checkForLine() defined (the one already not in the file):
private void saveToFile() {
try{
// Create file
BufferedWriter out = new BufferedWriter(new FileWriter(getCacheDirectory() + "output.txt"));
for(String a : monstersToAdd){
out.write(a);
out.newLine();
log("Wrote " + a + "to file");
}
//Close the output stream
out.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
}
The order of execution:
getNPCS();
getNames(monsterList);
getLevels(monsterList);
getIDs(monsterList);
combineInfo();
checkForLine();
saveToFile();
The problem however, is that it doesn't correctly check for the .txt file for information. I can see that because it just saves whatever it observes over and over again, not sorting anything away. This was the only way I could think of with my limited knowledge, and it didn't work.
For those wondering: This is a script for a bot called RSbot that plays the game called RuneScape. I don't actually use the bot, but I wanted to do this for the exercise.
I can paste the entire script if that would clear up things further.
I really appreciate any help, and will of course remember to select the answer that I used (if anyone bothers helping out ;) ).
Thanks.
for(String monster : monstersToAdd){
if(line.contains(monster)){
monstersToAdd.remove(monster);
}
}
will throw a ConcurrentModificationException
if line.contains(monster)
is true
, and monstersToAdd
contains monster
. The only safe way to remove an element from a collection while iterating is to use Iterator
:
for(Iterator<String> iter = monstersToAdd.iterator(); iter.hasNext();){
String monster = iter.next();
if (line.contains(monster)) iter.remove();
}
Edit
@trutheality points out
Actually, a much simpler way to accomplish the same thing is
monstersToAdd.removeAll(line);
So you can replace the for
loop with one line of code.
One possible problem is that you seem to be overwriting the same file when you "save". I suggest making a test run where you read from one file and write to the other.
In order to append to the file, you have a couple of options:
- Have your two functions share the 'RandomAccessFile`, that way after the first one is done reading the file, the cursor is at the end of the file and the second function can proceed to write from there, appending to the file
- Open a
RandomAccessFile
in the second function and move the cursor to the end of the file (for example by reading everything in it until there are no more lines) before writing.
精彩评论