#getting out of bounds error, but not sure how to fix

10 messages · Page 1 of 1 (latest)

floral salmon
#

i'm trying to make iterative tower of hanoi, here is everything i have done so far

import java.lang.Math;
import java.util.Scanner;
public class Hanoi {
    public static void hanoi(int n) {
        // move n disks from the source peg to the dest peg using spare peg.
        if(n == 0)
            return;
        if(n>26)
            return;
        Character[] stackArray = new Character[n+1];
        Stack<Character> A = new Stack<Character>(stackArray);
        Stack<Character> B = new Stack<Character>(stackArray);
        Stack<Character> C = new Stack<Character>(stackArray);
        for(int i = 0; i<n; i++)
            A.push((char)('a'+i));
        for(int j = 0; j<=Math.pow(2, n)-1; j++) {
            if(A.top() == 'z') {
                if(j%3==1 && !A.isEmpty()) {
                    C.push(A.pop());
                    System.out.println("Disk has been moved to pole 3");
                }
                else if(j%3==2 && !A.isEmpty()) {
                    B.push(A.pop());
                    System.out.println("Disk has been moved to pole 2");
                }
                else if(j%3==0 && !C.isEmpty())
                    B.push(C.pop());
                    System.out.println("Disk has been moved to pole 2");
            }
            
            else {
                if(j%3==1 && !C.isEmpty()) {
                    A.push(C.pop());
                    System.out.println("Disk has been moved to pole 1");
                }
                else if(j%3==2 && !A.isEmpty()) {
                    B.push(A.pop());
                    
mortal cliffBOT
#

This post has been reserved for your question.

Hey @floral salmon! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

floral salmon
#

second half of the code is here (since it has 2000 character limit)

#
System.out.println("Disk has been moved to pole 2");
                }
                else if(j%3==0 && !C.isEmpty()) {
                    B.push(C.pop());
                    System.out.println("Disk has been moved to pole 2");
                }
            }
        }
    }
    
    public String toString(Stack stack1, Stack stack2, Stack stack3) {
        return 
    }
    /*hanoi(n-1, source, spare, dest);
    System.out.println("Move on disk from " + source + " to " + dest
               + ".");
    hanoi(n-1, spare, dest, source);
    }
    */
    public static void main(String[] args) {
        System.out.print("Enter number of disks: ");
        Scanner stdin = new Scanner(System.in);
        int diskNumber = stdin.nextInt();
        hanoi(diskNumber);
    }
}
#

my code for my stack:

public class Stack<E> {
    private E[] els;
    private int count;
    
    public Stack(E[] els0) {
        els = els0;
        count = 0;
    }
    
    public void push(E e) { 
        els[count++] = e; 
    }
    
    public E pop() { 
        return els[--count]; 
    }
    
    public boolean isEmpty() { 
        return count == 0; 
    }
    
    public boolean isFull() { 
        return count==els.length; 
    }
    
    public E top() { 
        return els[count-1]; 
    }
}
#

the error is at line 17, or

        if(A.top() == 'z') {
#

but i'm not sure how i'd change it to fix it

#

it gives -1 for A.top()

desert dagger
#

well you need to handle condition when your stack is empty