I'm writing a deque implementation with ArrayList as required by my instructor. So far the body of the class looks like this

try {
            while (!endOfFile) {
                character = inputFile.readChar();
                while (!character.equals('\u0003')) {
                    if (character.equals('\u0008'))
                        deck.removeBack();
                    else
                        deck.addToBack(character);
                }
            }
            
            while (!deck.isEmpty()) {
                character = deck.removeFront();
                if (character.equals('\u0003'))
                    System.out.print("\n");
                else
                    System.out.print(character);
            }
        } catch (EOFException e) {
            endOfFile = true;
        }

The deque is initialized as

Deque<Character> = new deck Deque<Character>()

I've tested my Deque with a separate test class and I'm quite sure that it's working properly. But every time I try to run this read class, it causes a java.lang.OutOfMemoryError at the deck.addToBack(character) line. What is causing the problem and how can it be avoided?

Edit: In case it may be a problem with my implementation, I'm adding my Deque class code here. The interface was provided by my instructor.

import java.util.*;
public class Deque<T> extends ArrayList<T> implements DequeInterface<T> {
    
    public Deque()
    {
       super();
    }

    public void addToFront(T newEntry) {
        add(0, newEntry);
    }
    
    public void addToBack(T newEntry) {
        add(newEntry);
    }
    
    public T removeFront() {
        T entry = null;
        entry = get(0);
        remove(0);
        return entry;
    }
    
    public T removeBack() {
        T entry = null;
        entry = get(size() - 1);
        remove(size() - 1);
        return entry;
    }
    
    public T getFront() {
        T entry = get(0);
        return entry;
    }
    
    public T getBack() {
        T entry = get(size() - 1);
        return entry;
    }
    
    public boolean isEmpty() {
        if (size() == 0)
            return true;
        else
            return false;
    }
    
    public void clear() {
        clear();
    }
}

Do you have an infinite loop or a recursive call?
How many times does your code loop in the while() loop? What causes/lets the loop end?

I'm thinking it's probably an unintended infinite loop somewhere. The code is supposed to loop only as long as there is data to read in the .dat file. It should end when it reaches the end of the file. Is there a specific code to tell it that the end of file has been reached?

You don't need two while loops for populating your Deque; also your logic is flawed at this place:

character = inputFile.readChar();
                while (!character.equals('\u0003')) {
                    if (character.equals('\u0008'))
                        deck.removeBack();
                    else
                        deck.addToBack(character);
                }

As soon as you read a character which is not '\u0003', the control enters the second `while` and never moves out since you don't change `character` inside the loop.

Since you have your catch block outside your logic, an EOFException would cause the control to move out of your logic and never execute the part which follows.

Also, I hope you are reading a char which was written using writeChar; if not, use a Scanner for reading simple text files.

Is there a specific code to tell it that the end of file has been reached

Have you read the the API doc for the class and method you are using to read from the file? It should be explained there.

Yes, I've read the docs for the classes I'm using, but I don't recall seeing methods that tell it is the end of the file. Maybe I should look it up again.

So the problem is with the loop I'm using? I wrote this based on the algorithm the instructor gave me, so some of it isn't what I would normally do and I'm not very experienced at using multiple loops yet. The binary file was also provided, and I tried to read it with simpler code that prints the chars as they come with no problems, so it definitely is readable with readChar. Should I add another read method to change 'character' at the end of the loop?

~s.o.s~ explained how you get into an infinite loop in a while loop.
You need to provide an exit from the loop.

Thanks everyone, I finally got it to work. I removed the nested while and replaced it with a standard if statement. I also removed the output block from the try block and left it outside the catch. It now displays the data file contents correctly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.