Results 1 to 5 of 5
  1. #1
    laMonaca is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Problems with using other classes methods/instances

    Hi, I'm new to Java, and I've got a problem with it.

    I'm writing a class that reverts the content of a stack;

    In the folder there are: ReverseStackUsingTwoStacks.java
    ArrayListStack.java ==> Interface that wraps ArrayList, making an ArrayList instance a Stack
    ArrayListStack.class

    When I try to compile the code you're reading under my post, the compiler says

    "error: cannot find symbol
    ArrayListStack temp2 = new ArrayListStack<Integer>();
    ^
    symbol: class ArrayListStack
    location: class ReverseStackUsingTwoStacks"

    Here is the code:

    Java Code:
    import static java.lang.System.*;
    import java.util.Scanner;
    
    
    
    public class ReverseStackUsingTwoStacks
    {   public static void main(String[] args)
    	{    Scanner in = new Scanner(System.in);
    	     ArrayListStack<Integer> stack = new ArrayListStack<Integer>();
    	     while (in.hasNextInt())
    	     {    stack.push(in.nextInt());
    		 }
    
    		 StackInverter(stack);
    		 while (!stack.isEmpty())
    		 out.println(stack.pop());
    	}
    
    	public static void StackInverter(ArrayListStack<Integer> stack)
    	{    ArrayListStack temp1 = new ArrayListStack<Integer>();
    	     ArrayListStack temp2 = new ArrayListStack<Integer>();
    
    	     while (!stack.isEmpty())
    	     {    int element = stack.pop();
    	          temp1.push(element);
    		 }
    
    		 while (!temp1.isEmpty())
    		 {    int element = temp1.pop();
    		      temp2.push(element);
    		 }
    
    		 while (!temp2.isEmpty())
    		 {    int element = temp2.pop();
    		      stack.push(element);
    		 }
    	}
    
    
    
    }
    PS do not focus on single programming error, my problem is that even when creating an instance of ArrayListStack, the compiler says he do not find the symbol etc etc.

    Thanks.
    Last edited by laMonaca; 01-19-2013 at 12:54 PM. Reason: Wrapped code into code tags

  2. #2
    milovan is offline Senior Member
    Join Date
    Jan 2011
    Location
    Belgrade, Serbia
    Posts
    292
    Rep Power
    4

    Default Re: Problems with using other classes methods/instances

    please wrap your code in code tags.

  3. #3
    laMonaca is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problems with using other classes methods/instances

    Done, sorry, I've never even posted a piece of code before.

  4. #4
    huntack is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: Problems with using other classes methods/instances

    It seems from the pasted code that you forgot to import the class for ArrayListStack. Does it keep failing to compile when you do import it?

  5. #5
    laMonaca is offline Member
    Join Date
    Jan 2013
    Posts
    3
    Rep Power
    0

    Default Re: Problems with using other classes methods/instances

    Quote Originally Posted by huntack View Post
    It seems from the pasted code that you forgot to import the class for ArrayListStack. Does it keep failing to compile when you do import it?
    But, I knew that a class in the same directory has not to be imported, it is not in a package...
    There's the interface:

    Java Code:
    import java.util.ArrayList;
    public class ArrayListStack<T>
    {  private ArrayList<T> a;
       public ArrayListStack()     { a = new ArrayList<T>(); }
       public int size()           { return a.size(); }
       public boolean isEmpty()    { return a.isEmpty(); }
       public void push(T element) { a.add(element); }
       public T top()
       {  if (isEmpty()) throw new java.util.NoSuchElementException();
          return a.get(a.size()-1);
       }
       public T pop()
       {  if (isEmpty()) throw new java.util.NoSuchElementException();
          return a.remove(a.size()-1);
       }
    }


    !!!!SOLVED!!!!
    I just added the whole C: to Classpath system variable, now it compiles. Sorry for the disturb. Thanks.
    Last edited by laMonaca; 01-19-2013 at 02:00 PM.

Similar Threads

  1. Help with instances of classes and variables!
    By lifedistroy in forum New To Java
    Replies: 5
    Last Post: 12-24-2012, 07:15 AM
  2. Replies: 2
    Last Post: 01-22-2012, 11:50 PM
  3. Creating new instances in a loop problems
    By monkeyhead in forum New To Java
    Replies: 6
    Last Post: 11-18-2011, 09:14 PM
  4. Replies: 6
    Last Post: 10-02-2011, 05:55 PM
  5. Deleting instances of classes from a list
    By Gmurph03 in forum New To Java
    Replies: 8
    Last Post: 11-27-2009, 11:41 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •