The ProgrammersTalk Community
Forum Register Search Today's Posts Mark Forums Read
Register

Go Back   The ProgrammersTalk Community > General Programming > Java


Welcome to the The ProgrammersTalk Community forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.
Reply
 
LinkBack Thread Tools    Display Modes   
  #1 (permalink)  
Old 07-23-2007, 10:52 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Icon9 Why does java.util.EmptyStackException get thrown in this code?

Here's the first code that I had:

PHP Code:
                    for (int i 0< array.lengthi++) {
                        
Object o1 s.pop();
                        
Object o2 q.dequeue();
                        
String num1 = (String) o1;
                        
String num2 = (String) o2;
                        if (!
num1.equals(num2)) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    } 
I have no clue why does this get thrown:

Code:
Exception in thread "main" java.util.EmptyStackException
    at java.util.Stack.peek(Unknown Source)
    at java.util.Stack.pop(Unknown Source)
    at MidtermPracticeMain.<init>(MidtermPracticeMain.java:48)
    at MidtermPracticeMain.main(MidtermPracticeMain.java:104)
I changed it to this one:

PHP Code:
                    for (int i 0s.size()-1i++) {
                        if (!
s.pop().equals(q.dequeue())) {
                            
result "FALSE";
                        } else {
                            
result "TRUE";
                        }
                    } 
Exception doesn't get thrown, (but I still got logic error lol..)
My question is, how can they be different? What's wrong with the first one compared to the second one? Aren't they the same? sigh... obviously not, but I still can't see the difference...

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #2 (permalink)  
Old 07-23-2007, 11:35 PM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Maybe it has something to do with:
PHP Code:
for (int i 0< array.lengthi++) { 
vs
PHP Code:
for (int i 0s.size()-1i++) { 
? Without knowing what q, s, array, etc. are, it is somewhat difficult to help you. Please post a bit more code, so that we might better assist you.

Thanks,
rpgfan3233

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-23-2007)
  #3 (permalink)  
Old 07-23-2007, 11:38 PM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Thanx rpgfan3233
Yes, that's where the problem starts, it throws me exception if I changed it back to array.length

But, array.length should be the same with s.size() though because I created all of them within this for loop:

PHP Code:
                    StringTokenizer st = new StringTokenizer(line.trim(), " ");
                    
int cnt 0;
                    
int length 0;
                    while (
st.hasMoreTokens()) {
                        
String num st.nextToken();
                        
s.push(num);
                        
q.enqueue(num);
                        
v.add(num);
                        array[
cnt] = num;
                        
ll.add(num);
                        
cnt++;
                        
length++;
                    } 
I edited this already and all works perfectly fine with the length counter lol... logic works correctly, but still wondering why isn't that the same thing..

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
  #4 (permalink)  
Old 07-24-2007, 12:07 AM
rpgfan3233 rpgfan3233 is offline
PT Staff
Awards Showcase
Quality Tutorial Quality Tutorial Quality Tutorial Quality Tutorial 
Total Awards: 4
Join Date: Jul 2007
Posts: 118
iTrader: (0)
rpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura aboutrpgfan3233 has a spectacular aura about
Instead of testing against the size of the stack, why not use a while loop and test whether it is empty. I personally think it would be much safer than the for loop, though I honestly still don't see the difference with that code:
PHP Code:
while (!s.empty()) {
    
Object o1 s.pop();
    
Object o2 q.dequeue();
    
String num1 = (String) o1;
    
String num2 = (String) o2;
    if (!
num1.equals(num2)) {
        
result "FALSE";
    } else {
        
result "TRUE";
    }

I honestly can't see any problem from the code you've presented.

__________________
"C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off."
-- Bjarne Stroustrup, creator of what is now known as C++
For more quotes by Bjarne Stroustrup, check out http://www.research.att.com/~bs/bs_faq.html#really-say-that.
Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
The Following User Says Thank You to rpgfan3233 For This Useful Post:
HelloWorld (07-24-2007)
  #5 (permalink)  
Old 07-24-2007, 12:10 AM
HelloWorld's Avatar
HelloWorld HelloWorld is offline
Programming Expert
Awards Showcase
Quality Tutorial 
Total Awards: 1
Join Date: Jun 2007
Location: In front of computer...
Posts: 1,109
iTrader: (0)
HelloWorld will become famous soon enoughHelloWorld will become famous soon enoughHelloWorld will become famous soon enough
Hey thanx a lot!!
I don't realize that I have this method in stack

PHP Code:
s.empty() 
Will try it out later. Now I gtg bed and do some math lol..

__________________
PHP Code:
System.out.println("Hello World!"); 

Digg this Post! Del.Icio.Us this Post! Technorati this Post! Furl this Post! Mister Wong this Post! Newsvine this Post! Spurl this Post! Reddit this Post! Netscape this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

   Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -7. The time now is 05:33 PM. Powered by vBulletin
Copyright © 2000 - 2007, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO © 2007 ProgrammersTalk Sedo - Buy and Sell Domain Names and Websites project info: programmerstalk.net Statistics for project programmerstalk.net etracker® web controlling instead of log file analysis


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50