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 6 of 6

Thread: Stack - For loop not returning proper result

  1. #1
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Stack - For loop not returning proper result

    Below is a method that is suppose to insert a value inside a stack based on the index. I thought it would be easier to copy the value, if any, that was in the index into the variable holder and replace it with the input value. After in which it would copy the holder value and place it at the top of the stack. (topOfStack variable is what the holder is copying too.)

    public void pushExact (int index, String input) {
    	String holder = "";
    	if (maxSize == 0) {
    		theStack[topOfStack] = input;
    		topOfStack++;
    	} else if (topOfStack + 1 < maxSize) { 
    		for (int n= maxSize - 1;n >= 0;n--) {
    			if (n == index) { //this isn't returning the proper result. Its taking the topOfStack and not factoring in the index. 
    				holder = theStack[topOfStack];
    				System.out.println("Holders" + " value is: " + holder);
    				theStack[topOfStack] = input;
    				}
    			}
    			theStack[topOfStack] = holder; 
    			System.out.println("bottom is: " + bottom);
    			System.out.println("topOfStack is: " + topOfStack);
    			topOfStack++;	
    	} else {
    		System.out.println("The Stack is full.");
    	}
    	display();
    }


  2. #2
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Stack - For loop not returning proper result

    Quote Originally Posted by Johnathanrs View Post
    Below is a method that is suppose to insert a value inside a stack based on the index.
    That does not make any sense whatsoever.
    The idea of a stack is that you cant insert values at random locations.
    With your method your data structure would no longer be a stack, so please clarify.

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stack - For loop not returning proper result

    You're describing a List, perhaps a linked list. Stacks and queues are data structures that do not insert elements in their middles.

  4. #4
    Junior Member
    Join Date
    Feb 2014
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Stack - For loop not returning proper result

    @Cornix
    Self-learning. I started off trying to learn what a stack is and diverged on my own path from the core lessons taught. Not focusing on terminology or what it is technically called at this point. Just trying to learn how to create random functions from top of my head. Forget that I called it a stack. What is exactly wrong with my method?

    @Greg
    I am learning about linked list atm, but I believe what my method is doing is slightly different, but theoretically similar to the concept to a linked list. However, I am not shifting elements, but swapping the index point, storing it in a random variable, and than swapping the random variable with the first or last element. My next task is to figure out how to shift every element over by one, but I am not at that point yet in my learning.

  5. #5
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Stack - For loop not returning proper result

    Interesting exercises. Good luck.

  6. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Stack - For loop not returning proper result

    The inside of your for-loop does not make any sense at all to me. Could you perhaps make a comment about every single line and tell us exactly what you are trying to do and what your idea was?
    			if (n == index) { //this isn't returning the proper result. Its taking the topOfStack and not factoring in the index. 
    				holder = theStack[topOfStack];
    				System.out.println("Holders" + " value is: " + holder);
    				theStack[topOfStack] = input;
    				}
    			}
    			theStack[topOfStack] = holder; 
    			System.out.println("bottom is: " + bottom);
    			System.out.println("topOfStack is: " + topOfStack);
    			topOfStack++;

Similar Threads

  1. what is difference between call stack and stack tace?
    By me_shankara in forum Exceptions
    Replies: 6
    Last Post: October 27th, 2018, 03:23 AM
  2. [SOLVED] getting the proper input from file
    By mia_tech in forum What's Wrong With My Code?
    Replies: 11
    Last Post: June 18th, 2012, 08:11 AM
  3. returning color of pixel in loop
    By Vergil333@gmail.com in forum Loops & Control Statements
    Replies: 2
    Last Post: March 6th, 2012, 09:14 AM
  4. Returning to a loop after showConfirmDialog.
    By qrts in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 5th, 2011, 07:01 AM
  5. making a sentence proper
    By dvsumosize in forum Algorithms & Recursion
    Replies: 5
    Last Post: February 3rd, 2010, 11:01 AM