Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6016045
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:55:36+00:00 2026-05-23T02:55:36+00:00

The actual problem will be addressed a bit further down :), thanks. I’m fairly

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:55:36+00:00Added an answer on May 23, 2026 at 2:55 am
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The actual problem I am using Rails 3.0.4 and MongoDB through mongoid. I am
I have the following code: The actual problem is the non-quoted code. I want
Ok, my actual problem was this: I was implementing an IList<T> . When I
This is more of a theoretical question than an actual problem I have. If
There is an actual running Java ServerPages (JSP) application within a *NIX box which
The actual problem I'm trying to solve is, I want to automatically find out
My problem can be easily defined with the following code: self.Bind(wx.EVT_MENU_OPEN, self.OnAbout) This will
I'm fairly new to the OO design process, so please bear with me.... I
Problem: Sometimes you will want to access a component from javascript with getElementById ,
Here is code in jsfiddle ! Here is the actual site ! Problem 1:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.