2

I am using a linked-list. The node class (for the linked-list) is like this:

public class node {
    String data;
    node next;
    node previous;
}

In class stack, which uses node class, I've written a method print() to print values, print() is like below:

void print() throws IOException{
        try(BufferedWriter print=new BufferedWriter(new OutputStreamWriter(System.out))){
            node temp=this.getFirstNode();
            while(temp!=null){
                print.write(temp.getData());
                temp=temp.getNext();
            }
            print.close();
        }
    }

When I create 2 instances of stack class and call print(), it just prints the data of 1st instance. For example in below code it doesn't print B's data:

public static void main(String[] args) {
        stack A=new stack();
        stack B=new stack();
        A.print();
        B.print();
    }

I've searched a lot and debugged it several times, it runs perfectly. but couldn't figure out why it doesn't print out data for the second time.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
HMD
  • 468
  • 1
  • 5
  • 21
  • 1
    I'd hazard a guess that your call to `print.close()` closes the default output stream `System.out`, once it's closed you cannot reopen it. – d.j.brown Oct 18 '16 at 08:21
  • @d.j.brown it is not like one stream for each instance?! it is a general stream that all instances use it?! – HMD Oct 18 '16 at 08:26
  • `System.out` is a static field in the `System` class and is shared by every class/object using it. Use `print.flush()` instead, don't close the stream. – d.j.brown Oct 18 '16 at 08:29
  • @d.j.brown i thought every instance have its own stream. thank you – HMD Oct 18 '16 at 08:30
  • Every instance of your stack class has its own `BufferedWriter` object, but the output stream `BufferedWriter` is using is shared in this case (`System.out`), once you close `BufferedWriter` it will also close the underlying output stream. Additionally, for reference, all class names in Java should begin with an uppercase character and then CamelCase. – d.j.brown Oct 18 '16 at 08:33
  • @d.j.brown what do u mean _for reference, all class names in Java should begin with an uppercase character_ ?! – HMD Oct 18 '16 at 08:36
  • your `stack` and `node` classes in your question should be `Stack` and `Node`, it's Java naming convention. – d.j.brown Oct 18 '16 at 08:37
  • @d.j.brown yes, i know it, these were just examples, in my actual code i always use uppercase characters. but i didn't understand why it is necessary for reference. if i don't use UC characters, there would be problem afterwards?! – HMD Oct 18 '16 at 08:41
  • I just made the comment about naming convention in case you were not aware, I'm only trying to help. – d.j.brown Oct 18 '16 at 08:45
  • @d.j.brown i know, i didn't mean that way, thank u again. my question is just about this expression in your answer: _for reference_ . i can't figure it out! – HMD Oct 18 '16 at 08:49
  • sorry it may be lost in translation, what I mean to say is 'for your information'. – d.j.brown Oct 18 '16 at 08:50
  • @d.j.brown got it, thanks. :) – HMD Oct 18 '16 at 08:53

1 Answers1

4

You've closed System.out, which is something you shouldn't usually do. While using the try-with-resource syntax is usually the best practice in Java, when dealing with System.out it's just redundant (read: usually wrong):

void print() throws IOException{
    BufferedWriter print = new BufferedWriter(new OutputStreamWriter(System.out));
    node temp = this.getFirstNode();
    while(temp != null){
        print.write(temp.getData());
        temp = temp.getNext();
    }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350