Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Java Generic Stack

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I'm having an issue with a stack program I wrote, if I enter 3 inputs for a string, EX: "one, two, three, quit" (quit is how all inputs are to end and doesn't get added to the stack) the program works totally fine.
But when I put more inputs, EX:"one,two,three,four,five,six,seven,eight,nine,quit" It doesn't printout all the values, for example, prints from "nine" to "three" but doesn't print "one" or "two".
Probably just a simple mistake, but I can't seem to find out why this is happening, any help is appreciated!

I will post the class and tester class I am using:






 
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're splitting the string on space characters, but your input doesn't contain spaces.
 
J Hiddleston
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You're splitting the string on space characters, but your input doesn't contain spaces.



Sorry, I understand this, just wasn't thinking when writing that part of the post.
I am entering inputs as follows, " one two three four five six" etc...
Apologies for not cluing in whilst writing.
 
Stephan van Hulst
Saloon Keeper
Posts: 12431
269
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It probably has to do with how you're using the stack size in your for loop condition while popping elements at the same time. Each iteration, i gets incremented, but the stack size becomes smaller as well. The only reason it work for a stack size of three is because you have the +2 in your loop condition, which doesn't make sense to use in the first place.

You probably want the loop to look like this instead:
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic