Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    import java.io.*;
    public class StackImplementation
    {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    int max;
    int top;
    int stack[];

    public StackImplementation()
    {
    top=0;
    max=0;
    stack=new int[max];
    }

    public void Push(int num)
    {
    System.out.println("Top: "+top+"and max: "+max);
    if(top==max)
    System.out.println("STACK OVERFLOW!!!");
    else
    {
    stack[top]=num;
    top++;
    System.out.println(num+" is pushed in the stack!");
    }
    }

    public void Pop()
    {
    if(top==0)
    System.out.println("STACK UNDERFLOW!!!");
    else
    {
    stack[top]=0;
    top--;
    System.out.println("Element poped!!!");
    }
    }

    public void display()
    {
    if(top==0)
    System.out.println("STACK UNDERFLOW!!NO ELEMENTS TO SHOW!!");
    else
    {
    for(int i=top;i>0;i--)
    {
    if(stack[i]!=0)
    System.out.print(stack[i]+" ");
    else
    System.out.print("");
    }
    }
    }

    public void main(String args[]) throws IOException
    {
    StackImplementation obj= new StackImplementation();
    System.out.println("----STACK AS AN ARRAY IMPLEMENTATION----");
    max=Integer.parseInt(br.readLine());
    System.out.println("MAX:"+ max);
    int inpt=0,n;
    do
    {
    System.out.println("1)Enter 1 for performing Push\n2)Enter 2 for performing Pop\n3)Enter 3 for Displaying\n"
    +"4)Enter 4 for Exiting");
    inpt=Integer.parseInt(br.readLine());
    switch(inpt)
    {
    case 1:
    System.out.println("Enter number to be Pushed :");
    n=Integer.parseInt(br.readLine());
    obj.Push(n);
    break;
    case 2:
    obj.Pop();
    break;
    case 3:
    obj.display();
    case 4:
    System.out.println("******Program Terminates******");
    System.exit(0);
    default:
    System.out.println("Wrong Entry!!");
    }
    }while(inpt!=4);
    }
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    Can you explain what the problem is? What does the code do and what do you want it to do differently?

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    Well,
    this is a stack as an array program.When i call the push function for the first time, the 'max' value gets changed to zero, even though its a global variable, and i accept its value from the user. Any suggestion?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    Can you copy the program's output that shows what the problem is and paste it here?

    'max' value gets changed to zero, even though its a global variable
    Being global does not prevent a variable's value from changing. If you don't want a variable's value to change make it final.
    The default value for an int variable is 0. max will start with a value of 0.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    how do i make it final? i have to accept the max value in the main method, and after doing dat how shall i make it final? Btw, can u tell me why does the global variable's value change in the other method?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    how do i make it final?
    Use the final keyword when the variable is defined:
    final int max = 1234;
    why does the global variable's value change
    Where is the variable named: global defined? I don't see any variables with that name in the code.
    What variable are you asking about? Why do you call them: global?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    Capture.jpg
    Sorry for not being clear.The 'global' variable i was referring to was 'max'. If you see the output snapshot, when i call the Push() method, max's value gets changed to zero, even though i accepted it's value from user(6 in this case). I wanna know why that is happening.
    Attached Images Attached Images

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,077
    Thanks
    47
    Thanked 2,147 Times in 2,120 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    when i call the Push() method, max's value gets changed to zero,
    Can you copy the full contents of the command prompt window that shows what you are saying and pasted it here? No images because text can not be copied from images when making a response.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Dec 2013
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Hi guys, can any one tell what's wrong with my code?This is about stack as arrays and this is based on bluej environment

    hii....
    initially initialise top = -1 and variable max with some value......
    suppose max = 4;
    ...removed spoonfed code
    Last edited by copeg; December 29th, 2013 at 11:38 PM. Reason: removed spoonfed code

Similar Threads

  1. What Am I Doing Wrong?(Arrays)
    By zendorz in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2013, 03:41 AM
  2. What's wrong with my code? (generate sequences of numbers using a stack)
    By mescoff in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 18th, 2012, 09:12 AM
  3. Radio button hide based on input..please help guys
    By ravi9999 in forum JavaServer Pages: JSP & JSTL
    Replies: 0
    Last Post: April 7th, 2012, 06:24 AM
  4. What am I doing wrong with this stack? Java Programming?
    By babe20042004 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 1st, 2011, 12:19 PM
  5. help guys.. could you tell me what am i doing wrong?
    By gonfreecks in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 12th, 2011, 01:59 PM

Tags for this Thread