1

I am new to java and stackOverflow so please be patient if I don't post all the necessary information about my question. Basically, I am trying to read lines from a .txt file and store them in a Stack. I also need to access this Stack in a different class. So I created a get method but it always returns null. Please help!

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Scanner;
import java.util.Stack;

public class Hints {

    private Stack stack;
    private File f;
    private String line;
    private Scanner scanner;

    public Hints(){          
        f = new File("hints.txt");
        stack = new Stack();    
    }

    public void getList() throws FileNotFoundException, IOException {

        scanner = new Scanner(f);

        while(scanner.hasNextLine()) {
            line = scanner.nextLine();
            stack.add(line); 
        }
        scanner.close();
    }

    public Stack getStack(){
        return stack;
    }

}

When I try to print the stack with a simple System.out.print, it will come out as null. Where is my issue(s)? Thank you.

6
  • 2
    Show the code that doesn't work. Commented Feb 16, 2015 at 7:47
  • Please show your print code. stack should not be null. Commented Feb 16, 2015 at 7:47
  • Can you show the code that prints the stack? Commented Feb 16, 2015 at 7:47
  • Your code looks almost ok. So your issue must be with print method. Believe you defined it in another class and trying to create instance of Hint there which will print empty stack and not null. Commented Feb 16, 2015 at 7:50
  • When I call the getStack() method in another class is comes up empty. I can post another class where I use the method but I can only assume that the issue is coming from where I create the method and not where I am calling from. Am I mistaken about that? Commented Feb 16, 2015 at 8:02

3 Answers 3

1

Your code works fine. I think you are not calling the getList() method before you call the getStack() method.

try {
        Hints hints = new Hints();
        hints.getList();        // adds to the stack
        Stack s = hints.getStack();  // return the stack
        int stackSize = s.size();

        for (int i = 0; i < stackSize; i++) {

            System.out.println(s.pop());        // pop from the stack
        }
    } catch (IOException ex) {
        Logger.getLogger(JavaApplication18.class.getName()).log(Level.SEVERE, null, ex);
    }

You must call the getList() method first before you call the getStack() method. Because getList() method adds values that are read from the txt file. Then only you can call the getStack() method. Otherwise you don't have any values in the stack.

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

Comments

0

The code you presented looks fine so far. You initialize your stack by iterating through your textfile. So if the textfile contains lines, the stack will be filled with the data. So you might probably have a problem while printing your result.

Since java.util.Stack is derived from Vector, you can use the get(int) Method to access the data inside your stack. Therefore, you simply need a printing method that accesses the stack like this:

Hints h = new Hints();
h.getList();
Stack theStack = h.getStack();
for (int i = 0; i < theStack.size(); i++) {
    System.out.println(theStack.get(i));
}

Now your stack should be printed correctly to the console.

And remember calling the getList() method before trying to access your stack. You should consider initializing the stack within the constructor. Maybe this is the thing you are missing.

2 Comments

Thank you for the reply. But where you getElement() come from? It does not seem to a method that is part he stack methods. Could it be get()?
@HenriqueAguiar Yes it is, I updated my answer. Didn't work with the java stack for a long time ;-). Here are the docs of the stack: docs.oracle.com/javase/7/docs/api/java/util/Stack.html. As you found out, it is "get()" and not "getElement()".
0

This is my hint

 public Hints() throws FileNotFoundException, IOException{          
    f = new File("hints.txt");
    stack = new Stack();   
    getList(); // call get list in constructor.
 }

When you create the hint its populate the stack.

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.