Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: What am I doing wrong with this stack? Java Programming?

  1. #1
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default What am I doing wrong with this stack? Java Programming?

    I have a program that reads the names of countries from a file, prints them to the screen then prints thm from the stack. However when it prints from the stack this is the resulting output:

    Returning what is in the stack

    Country@42e816
    Country@9304b1
    Country@190d11
    Country@a90653

    Can someone help me please?
    This is the code:
    public class Country {
    	String name;
     
    }
    import java.io.*;
     
    public class StackOfObjects {
        private Node first;
     
     
        private class Node {
            private Object item;
            private Node next;
        }
     
     
        public StackOfObjects() {
            first = null;
        }
     
     
        public boolean isEmpty() { return (first == null); }
     
     
        public void push(Object item) {
            Node oldfirst = first;
            first = new Node();
            first.item = item;
            first.next = oldfirst;
        }
     
     
        public Object pop() {
            if (isEmpty()) throw new RuntimeException("Stack underflow");
            Object item = first.item;      
            first = first.next;            
            return item;                   
        }
     
     
        // a test client
        public static void main(String[] args) {
            StackOfObjects stack = new StackOfObjects();
     
     
     	   try{
     
     		    FileInputStream fstream = new FileInputStream("textfile.txt");
     
     		    DataInputStream in = new DataInputStream(fstream);
     		        BufferedReader br = new BufferedReader(new InputStreamReader(in));
     		    String strLine;
     		   System.out.println ("Returning what is in the file\n");
     
     		    while ((strLine = br.readLine()) != null)   {
     
     			      System.out.println (strLine);
     		    	Country count=new Country();
     		    	count.name=strLine;
     		    	stack.push(count);
     
     
     		    }
     		   System.out.println ("\nReturning what is in the stack\n");
     		    while( ! stack.isEmpty() ){
     		    	Object s=(Object) stack.pop();
     		        System.out.println(s);
     		    }
     
     
     		    in.close();
     		    }catch (Exception e){
     		      System.err.println("Error: " + e.getMessage());
     		}
     
        }
    }


  2. #2
    Member
    Join Date
    Apr 2011
    Posts
    32
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: What am I doing wrong with this stack? Java Programming?

    It appears to be returning the memory addresses of the items in the stack...I think. I've had this happen a few times before. Try
    System.out.println ("\nReturning what is in the stack\n");
     		    while( ! stack.isEmpty() ){
     		    	Object s=(Object) stack.pop();
     		        System.out.println(String.ValueOf(s));
     		    }

  3. The Following User Says Thank You to techwiz24 For This Useful Post:

    babe20042004 (May 1st, 2011)

  4. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: What am I doing wrong with this stack? Java Programming?

    Passing an object to a PrintStream's print or println will result in the Object's toString() method to be printed. See the API for details on what is printed (Object (Java Platform SE 6) ). In other words, if you want to print more detailed information, override the toString method and return a more detailed string.

  5. The Following User Says Thank You to copeg For This Useful Post:

    babe20042004 (May 1st, 2011)

  6. #4
    Junior Member
    Join Date
    Mar 2010
    Posts
    12
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: What am I doing wrong with this stack? Java Programming?

    Thanks to all! I've gotten it to work!
    while( ! stack.isEmpty() ){
    Country s=(Country) stack.pop();
    System.out.println(s.name);
    }

Similar Threads

  1. Something wrong with my programming, please help
    By mrmodest in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 5th, 2011, 08:33 PM
  2. Replies: 10
    Last Post: April 5th, 2011, 09:09 AM
  3. Socket programming something gone wrong
    By ambika.th in forum Java Networking
    Replies: 1
    Last Post: March 6th, 2011, 10:47 AM
  4. Custom Java stack class (with generics) problem
    By TBBucs in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 7th, 2010, 01:25 AM