help with java array

syn3rgyz

Gawd
Joined
Jun 14, 2005
Messages
763
so my professor gave me this class
Code:
import java.util.Scanner;
public class ABStack // Array-based implementation of a stack data structure
{
	//instance variables 

    int [] pile; // This array holds the contents of the stack. The stack grows rightward from element 0.
    int count; // # of elements in stack. This value is also the index of the next available array element.

    public ABStack(int size) //constructor
    {
		if(size > 1)
		    pile = new int[size];
		else
		    pile = new int[1];
	
		count = 0;
    }

    public boolean isEmpty()
    {
		return(count==0);
    }

    public boolean isFull()
    {
		return(count==pile.length);
    }	

    public int size()
    {
		return count;
    }

	// Add a new value to the top of the stack.
	// Do nothing if the stack is full.
    public void push(int n)
    {
		//add n to stack top
		if(!isFull()) //have room
		{
	    	pile[count]=n;
	    	count++;
		}
    }

	// Remove (and return) the value on the top of the stack.
	// If the stack is empty, return -99999 as a sentinel value.
    public int pop()
    {
		if(isEmpty())
		    return -99999;
		else
		{
		    count--;
		    return pile[count];
		}	    
    }

}

im trying to call the methods the code i have so far is
Code:
import java.util.*; // Needed for the Scanner class
public class lab12 
{
	
	public static void main(String[] args) 
	{
		
		Scanner sc = new Scanner(System.in);
		System.out.println("enter an expression in postfix notation");
		String something = sc.nextLine();
		char[] expression = something.toCharArray();
		ABStack stack = new ABStack(expression.length);
		for(int i = 0; i < expression.length; i++) 
		{
			int op1 = 0;
			int op2 = 0;
			if (expression[i] == '+')
			{
				op2 = stack[stack.length-1]
			}
				
		}

	}
}
i thought i used to constructor ABStack to create a new array with the length of 'expression' but i cant call the stack it claims its not an array. help plz?

*edit
now i tried calling the push method and it says its undefined too
Code:
import java.util.*; // Needed for the Scanner class
public class lab12 
{
	
	public static void main(String[] args) 
	{
		
		Scanner sc = new Scanner(System.in);
		System.out.println("enter an expression in postfix notation");
		String something = sc.nextLine();
		char[] expression = something.toCharArray();
		ABStack stack = new ABStack(expression.length);
		for(int i = 0; i < expression.length; i++) 
		{
			int op1 = 0;
			int op2 = 0;
			if (expression[i] != '+' && expression[i]!= '-' && expression[i] != '*' && expression[i] != '/')
			{
				push(expression[i]);
			}
				
		}

	}
}
 
thanks, but now i got another problem... so basically im trying to make the program understand 33+ as 3 + 3 and 32* as 3 * 2 etc..
my code is now
Code:
import java.util.*; // Needed for the Scanner class
public class lab12 
{
	
	public static void main(String[] args) 
	{
		Scanner sc = new Scanner(System.in);
		System.out.println("enter an expression in postfix notation");
		String something = sc.nextLine();
		char[] expression = something.toCharArray();
		ABStack stack = new ABStack(expression.length);
		for(int i = 0; i < expression.length; i++) 
		if (stack.count != 1)
		{
		{
			int op1 = 0;
			int op2 = 0;
			if(expression[i] == '+')
			{
				op2 = stack.pop();
				op1 = stack.pop();
				stack.push(op1 + op2);
				
			}
			else if(expression[i] == '-')
			{
				op2 = stack.pop();
				op1 = stack.pop();
				stack.push(op1 - op2);
				
			}
			else if(expression[i] == '*')
			{
				op2 = stack.pop();
				op1 = stack.pop();
				stack.push(op1 * op2);
			}
			else if(expression[i] == '/')
			{
				op2 = stack.pop();
				op1 = stack.pop();
				stack.push(op1 / op2);
			}
			else
			{
				stack.push(expression[i]);
				
			}
				
		}
		}
		else
			{
			System.out.print(stack.pop());
			}

	}
}
but i keep getting the wrong numbers as output i totally dont see whats wrong
 
OMG>>>!! how did i not see that... thanks,
but how do i convert back from char to int?
 
OMG>>>!! how did i not see that... thanks,
but how do i convert back from char to int?
Subtract character '0' from the character you want to convert. For example:
Code:
int r = '6' - '0';
r would now be the integer 6.
 
thanks for the replies i asked my TA for help and even he cant tell why my numbers keep comming out wrong. all he knows is somehow the numbers are in ascii code
 
thanks for the replies i asked my TA for help and even he cant tell why my numbers keep comming out wrong. all he knows is somehow the numbers are in ascii code

so have you tried implementing JasonE4's suggestion yet?

there's also an "Integer.valueOf(char)" function i believe
 
yep i tried jasons method and still no good

and my TA tried the method you mentioned and others too.
my professor said hes gonna exempt this lab and hes going to post a sample solution
 
Back
Top