473,406 Members | 2,769 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Why is it that my stack isn't working?

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:
Expand|Select|Wrap|Line Numbers
  1. import java.io.*;
  2.  
  3. public class StackOfObjects {
  4.     private Node first;
  5.  
  6.  
  7.     private class Node {
  8.         private Object item;
  9.         private Node next;
  10.     }
  11.  
  12.  
  13.     public StackOfObjects() {
  14.         first = null;
  15.     }
  16.  
  17.  
  18.     public boolean isEmpty() { return (first == null); }
  19.  
  20.  
  21.     public void push(Object item) {
  22.         Node oldfirst = first;
  23.         first = new Node();
  24.         first.item = item;
  25.         first.next = oldfirst;
  26.     }
  27.  
  28.  
  29.     public Object pop() {
  30.         if (isEmpty()) throw new RuntimeException("Stack underflow");
  31.         Object item = first.item;      
  32.         first = first.next;            
  33.         return item;                   
  34.     }
  35.  
  36.  
  37.     // a test client
  38.     public static void main(String[] args) {
  39.         StackOfObjects stack = new StackOfObjects();
  40.  
  41.  
  42.         try{
  43.  
  44.              FileInputStream fstream = new FileInputStream("textfile.txt");
  45.  
  46.              DataInputStream in = new DataInputStream(fstream);
  47.                  BufferedReader br = new BufferedReader(new InputStreamReader(in));
  48.              String strLine;
  49.             System.out.println ("Returning what is in the file\n");
  50.  
  51.              while ((strLine = br.readLine()) != null)   {
  52.  
  53.                    System.out.println (strLine);
  54.                  Country count=new Country();
  55.                  count.name=strLine;
  56.                  stack.push(count);
  57.  
  58.  
  59.              }
  60.             System.out.println ("\nReturning what is in the stack\n");
  61.              while( ! stack.isEmpty() ){
  62.                  Object s=(Object) stack.pop();
  63.                  System.out.println(s);
  64.              }
  65.  
  66.  
  67.              in.close();
  68.              }catch (Exception e){
  69.                System.err.println("Error: " + e.getMessage());
  70.          }
  71.  
  72.     }
  73. }
  74.  
  75.  
  76.  
Expand|Select|Wrap|Line Numbers
  1. public class Country {
  2.     String name;
  3.  
  4. }
  5.  
May 1 '11 #1
1 1584
I got it to work
Expand|Select|Wrap|Line Numbers
  1. while( ! stack.isEmpty() ){
  2. Country s=(Country) stack.pop();
  3. System.out.println(s.name);
  4. }
May 1 '11 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Sebastian Aguilera | last post by:
Hi everyone. I have some troubles using the unserialize() function. I have serialized an array and that works perfect: echo '<input type="hidden" name="vardekedja" value="'.serialize($_POST...
6
by: SB | last post by:
This while loop keeps repeating even when a correct character is entered.... cout<<endl<<"What day would you like to schedule the appointment?"<<endl; cout<<endl<<"Enter 'M' for Monday, 'T' for...
6
by: saju.prabhakaran | last post by:
We have an application developed in c++ and the development environment is visual studio .net 2003. We have added a cpp program to the application which will create a log file containing the trace...
9
by: Simon | last post by:
I've got a simple and repetitive bit of code for a function in a C implementation of the card game 31s I'm working on. BTW, I am a bit of a novice at C; for the past couple of years I was using...
10
by: sp | last post by:
The application is written in Visual Basic / .NET and working without problems under Windows XP, Windows 2000, Windows 2003 but it isn't working under Windows ME and Windows 98 - the computer...
2
by: PrinceMhul | last post by:
var objListBox = document.getElementById('catalist'); var val = objListBox.value; var txt = dyevalue00.value; document.getElementById(val).value = txt; To explain real quick, this is for a...
0
by: DougRenwick | last post by:
I'm an trying to prevent my application from stealing the focus from the application that launched it. Overriding the ShowWithoutActivation property returns a value of true but does not prevent the...
3
by: girish574 | last post by:
After the integration of boost serialization code, the exceptions are not at all getting caught in my application. Im building on RHEL4 kernel 2.6.9-42.ELsmp and gcc version is 4.0.2. The...
7
by: nassausky | last post by:
I acquired a very basic redirect script from: http://www.minisitegallery.com/blog/php-javascript-countdown-script-with-timezone-setting.html which is supposed to count down to a specified date and...
12
by: Hendor | last post by:
Hi all. I've recently set up Apache 2.2 with PHP 5.2 and MySQL 5.1. I played around with SQL a bit, and now I'm trying to access it with PHP. I currently have the code: <?php #...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.