[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Non-popping linked list

Posted on 2006-04-27
5
Medium Priority
?
184 Views
Last Modified: 2010-03-31
I wrote code that reads lines of text from a file and pushes them on a stack that is a LinkedList.

the file "test.txt" essentialy looks like this:
---------------------------------
top
Abstract: . In this paper, ....
of the features drawn from the document itself.
We also present a preliminary experiment
bottom
--------------------------------
My error is a non-terminating loop the prints to the console "bottom"  over and over.

My error must be in the following part:
while (!listit.isEmpty())
      System.out.println(listit.top()); //linked_list.getFirst()
        listit.pop(); //linked_list.removeFirst()

"bottom" doesn't seem to be popping off causing the loop.

what is my errror?

0
Comment
Question by:kellyputty
  • 2
  • 2
5 Comments
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 2000 total points
ID: 16555371
You forgot braces to enclose both statements in the body of the loop:

while (!listit.isEmpty()) {
     System.out.println(listit.top()); //linked_list.getFirst()
        listit.pop(); //linked_list.removeFirst()
}

Jim
0
 

Author Comment

by:kellyputty
ID: 16555519
grrr...
Learning Java - O'Reilly pg92 shows while and do while w/o curly braces
and
Eclipse doesn't give you a compilation  error.

but I should have figured that out anyways considering all other expressions do and how else would it know scope.

duh,
thanx.
0
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 16555683
LOL. Sometimes the obvious isn't so obvious. Good luck. BTW, you knew Java has a Stack class, right?
0
 

Author Comment

by:kellyputty
ID: 16558007
nope, grrr ;) now I do.  I am in the learning curve...
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 16560204
>> Eclipse doesn't give you a compilation  error.

Because you can have a loop with/ without {} both. If its without {}, then only one statement is in the loop.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses
Course of the Month18 days, 19 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question