Stacks, java,stack cannot be resolved type ,
trying to make a stack that lets the users input how many numbers gonna be
in stack first in first out , with 2 methods push and pop , push puts
element on the stack and pop removes the element on top .Attempting to
call the push method and the stack have more spaces then an exception is
thrown. Attempting to call the pop and stack is empty then even if an
exception is thrown. The question is little hard to understand but thats
because of my english
Here is my main class (Application)
package com.example.undantag.main;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Stack2 st = new Stack2();
System.out.println("Stack "+st);
System.out.println("Please enter how many numbers you would like
to enter");
Scanner scan = new Scanner(System.in);
int totalnumber = scan.nextInt();
System.out.println("Please enter the number you would like to
begin");
int beginnumber = scan.nextInt();
showPush(st, beginnumber, totalnumber);
System.out.println("Please enter how many numbers you would like
to delete");
int deletenumber = scan.nextInt();
if(deletenumber > totalnumber){
throw new IllegalArgumentException("Invalid number");
}
else{
for(int i=0; i<=deletenumber; i++)
{
showpop(st);
}
}
}
}
This is the second class
package com.example.undantag.main;
public class Stack2 {
public static int i;
static int TotalNumber(int totalnumber){
return totalnumber;
}
static void showPush(Stack st, int a, int totalnumber){
for(int i = 0; i <=totalnumber; i++)
{
st.push(new Integer (a++));
System.out.println("Push("+a + ") ");
}
}
static void showpop(Stack st){
System.out.print("Pop: ");
Integer a = (Integer) st.pop();
System.out.println(a);
System.out.println("Stack: "+st);
}
}
No comments:
Post a Comment