• 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
  • Bear Bibeault
  • Junilu Lacar
  • Martin Vashko
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Scott Selikoff
  • salvin francis
  • Piet Souris

Stacks

 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I can't seem to figure out the reason why my code doesn't work. The instructions are: use ‘-’ to erase the last character typed and ‘$’ to erase the entire line so far entered. This program may only stack structures; no Java built-in arrays.  The attached picture is what is supposed to be printed out. The first output was correctly printed but then the second ouput is this:
Enter a line of text:
AP$$-Compp-utee-r Sic--cei--ience
Here is the line you entered:Computer Sicceiience
Again (y/n)?


output.PNG
[Thumbnail for output.PNG]
 
Marshal
Posts: 66525
251
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In my opinion, Java® doen't handle stacks at all well because there is no Stack interface.
Suggest:-
  • 1: Don't use Stack; use ArrayDeque.
  • 2: If you are emptying the stack after a '$', you can't use a String collection, but a collection of Characters.
  •  
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    We haven't started learning ArrayDeque yet. Is it wrong to use what I have? I prefer string that is why I did not use characters.
     
    Campbell Ritchie
    Marshal
    Posts: 66525
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Don't use screenshots, least of al when they appear to show your code working.
     
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:Don't use screenshots, least of al when they appear to show your code working.


    My code is not outputting the correct output. The screenshot is supposed to be what the output should be.
     
    Sheriff
    Posts: 6439
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Do you mean, Why doesn't the program ask "Enter a line of text" again?  Look at what your while loop is enclosing.  Does it include the "Enter a line of text" prompt?
     
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My output was incorrect because the second line of text wasn't changed correctly as shown in my explanation
     
    Knute Snortum
    Sheriff
    Posts: 6439
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I see you mean, Why doesn't two "-" remove two characters.
     
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yep
     
    Knute Snortum
    Sheriff
    Posts: 6439
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, think of it like this: you're going over the String backwards.  You encounter a "-".  This says to pop the stack twice, removing the next character.  But what if the next character is a "-"?
     
    Campbell Ritchie
    Marshal
    Posts: 66525
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Surely the next minus would have been popped already?
     
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't understand what I'm doing wrong. The while loop is running and should be popping off the second '-'.
     
    Knute Snortum
    Sheriff
    Posts: 6439
    172
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    But you're looking backwards through the String.
    So the first hyphen you encounter is the second hyphen in the String.
     
    Ana Smith
    Ranch Hand
    Posts: 347
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wait, but how I would reverse the string and delete the two hyphens?
     
    Marshal
    Posts: 14476
    240
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You are going about solving this the wrong way. Instead of pushing everything on the stack at once, you should instead get one character at a time from the input.

    1. If the character is “-“ then pop the stack, if possible.
    2. If the character is “$” then pop everything off the stack, if possible.
    3. Otherwise, push the character on the stack.

    I’ll leave it to you to figure out how to display the result in proper order.
     
    Campbell Ritchie
    Marshal
    Posts: 66525
    251
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Ana Smith wrote:We haven't started learning ArrayDeque yet.

    It is professional to learn around the subject, unless you have been given additional restrictions.

    . . . I prefer string that is why I did not use characters.

    And as you have seen, Strings didn't work at all well.
     
    Junilu Lacar
    Marshal
    Posts: 14476
    240
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don’t see anything wrong with using a Stack for this problem.
     
    girl power ... turns out to be about a hundred watts. But they seriuosly don't like being connected to the grid. Tiny ad:
    Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
    https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!