1

Hey I have this code here:

public class Levels {
boolean newGame = true;

public void newGame() {
    while (newGame) {
        int cLevel = 1;

        List<String> list = new ArrayList<String>();

        try {
            BufferedReader bf = new BufferedReader(new FileReader(
                    "src/WordGuess/ReadFile/LevelFiles/Level_" + cLevel
                            + ".txt"));
            String cLine = bf.readLine();
            while (cLine != null) {
                list.add(cLine);
            }
            String[] words = new String[list.size()];
            words = list.toArray(words);
            for (int i = 0; i < words.length; i++) {
                System.out.println(words[i]);
            }

        } catch (Exception e) {
            System.out
                    .println("Oh! Something went terribly wrong. A team of highly trained and koala-fied koalas have been dispatched to fix the problem. If you dont hear from them please restart this program.");
            e.printStackTrace();
        }
    }
}}

And it gives me this error:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.util.Arrays.copyOf(Unknown Source) at java.util.Arrays.copyOf(Unknown Source) at java.util.ArrayList.grow(Unknown Source) at java.util.ArrayList.ensureExplicitCapacity(Unknown Source) at java.util.ArrayList.ensureCapacityInternal(Unknown Source) at java.util.ArrayList.add(Unknown Source) at WordGuess.ReadFile.SaveLoadLevels.Levels.newGame(Levels.java:24) at Main.main(Main.java:29)

Can anyone help, please? Thank you!

3
  • What size is the file you are reading? java.lang.OutOfMemoryError is thrown when your computer runs out of memory because you are running really heavy programs in Java. Commented Jan 28, 2014 at 14:52
  • @migueljimenezz: In this case the length of the file is irrelevant, so long as there's at least one line - see my answer :) Commented Jan 28, 2014 at 14:53
  • 1
    Where is newGame set to false? What I see here is an infinite loop! Commented Jan 28, 2014 at 14:53

2 Answers 2

3

This is the problem:

String cLine = bf.readLine();
while (cLine != null) {
    list.add(cLine);
}

You're not reading the next line in your loop (the value of cLine never changes) - so it's just going to loop forever. You want:

String line;
while ((line = bf.readLine()) != null) {
    list.add(line);
}

(And as noted in comments, this is also in an infinite outer loop because newGame will stay true forever...)

Sign up to request clarification or add additional context in comments.

2 Comments

Alexis Lecrec G.S Thank you, fellas! You are all correct! I haven't created my false statement and changed the while to if for now. And you really helped me with the readLine() statement. I thought the method reads through all the lines but now I figured it out. How dumb of me it says readLINE as in one line. Sorry to bother you with these simple stuff. I'm new to this if i can rate you please tell me how :) And if you can tell me if the structure of my code is good for a word guessing game for now that will be appreciated too. Thank you again!
Don't worry, we've all started with the simple stuff ;)
1

You are reading one line and keep on adding it to list which is causing out of memory.
Modify your code as:

String cLine
while(cLine = bf.readLine() != null)
{
    list.add(cLine);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.