• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Program for implementing a stack

 
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello coders,
I am currently learning Java by self study.
I tried to write a program to implement a stack but it has a bug I am unable to solve. Kindly help me with that.
The bug is that whenever I choose an operation to perform, eg push, After performing the operation, the loop is executed once again and Invalid choice message appears, i.e. the default case. And then the loop again executes to choose further option. Kindly try running the program if you don't understand what I mean to say. Here is my code

Also please give me any tips about the programming style is use, if any. Thank you.
 
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Replace your read's with a Scanner

 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks. Haven't learned using scanners yet. I am reading the "Java A Beginner's guide 6th edition. Currently I am on chapter 7: Inheritance. Scanners are introduced in Chapter 10. Any ways thank you very much for helping
 
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Who wrote that book? What does it say about the Scanner#nextLine method?
 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is written by Herbert Schildt. I haven't read that topic yet. But here is the extract of basic usage of scanner in that book.

To use Scanner to read from the keyboard, you must first create a Scanner linked to
console input. To do this, you will use the following constructor:
Scanner(InputStream from)
This creates a Scanner that uses the stream specified by from as a source for input. You can
use this constructor to create a Scanner linked to console input, as shown here:
Scanner conin = new Scanner(System.in);
This works because System.in is an object of type InputStream. After this line executes,
conin can be used to read input from the keyboard.
Once you have created a Scanner, it is a simple matter to use it to read numeric input.
Here is the general procedure:
1. Determine if a specific type of input is available by calling one of Scanner’s
hasNextX methods, where X is the type of data desired.
2. If input is available, read it by calling one of Scanner’s nextX methods.
As the preceding indicates, Scanner defines two sets of methods that enable you to read
input. The first are the hasNext methods. These include methods such as hasNextInt( ) and
hasNextDouble( ), for example. Each of the hasNext methods returns true if the desired
data type is the next available item in the data stream, and false otherwise. For example, calling
hasNextInt( ) returns true only if the next item in the stream is the human-readable form of
an integer. If the desired data is available, you can read it by calling one of Scanner’s next
methods, such as nextInt( ) or nextDouble( ). These methods convert the human-readable
form of the data into its internal, binary representation and return the result. For example, to
read an integer, call nextInt( ).
The following sequence shows how to read an integer from the keyboard.
Scanner conin = new Scanner(System.in);
int i;
if (conin.hasNextInt()) i = conin.nextInt();
Using this code, if you enter the number 123 on the keyboard, then i will contain the value 123.
Technically, you can call a next method without first calling a hasNext method.
However, doing so is not usually a good idea. If a next method cannot find the type of
data it is looking for, it throws an InputMismatchException. For this reason, it is best to
first confirm that the desired type of data is available by calling a hasNext method before
calling its corresponding next method.

 
Sheriff
Posts: 7125
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There's a problem with Stack::push(). It's an off-by-one problem. Can you spot it?

 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
uhmmm... nope... I am unable to find any problem.
 
Carey Brown
Saloon Keeper
Posts: 10653
85
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Praneet Rohida
Greenhorn
Posts: 8
Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ohhh Thank you
 
Campbell Ritchie
Marshal
Posts: 79076
376
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I can also see problems with pop()

That Schildt quote is less than perfect, and it doesn't mention nextLine. You will end up with your input lost if you use hasNextInt like that.You should use a loop, as Rob Spoor showed me a long time agoI have reviewed a copy of Schildt's other book, and he seems to think that nextLine returns the next line, which is incorrect. In fact I don't remember a book which gets nextLine right.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Praneet Rohida wrote:Also please give me any tips about the programming style is use, if any.


Well, two leap out at me right off the top:

1. Your constructor takes an int called 'l', which is not generally a very good idea. Reason: It took me a little while to realise that you weren't allocating an array of length 1 (one).

2. Your strategy for "errors" isn't the best; especially for your pop() method. Currently, you print out a message and return a (presumably) invalid character -1 - which will actually be translated as 65535 (\uFFFF) because chars are unsigned. Far better would be to throw an exception; and the normal one in your case is IllegalStateException.

HIH

Winston
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic