Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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> 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.

like image 843
Mike Haye Avatar asked Jun 15 '11 19:06

Mike Haye


1 Answers

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.

like image 85
Matt Ball Avatar answered Nov 15 '22 00:11

Matt Ball