Please help me fix this. I think I messed it up. These are the errors I keep get
ID: 3545246 • Letter: P
Question
Please help me fix this. I think I messed it up.
These are the errors I keep getting for my program below.
6 errors found:
File: C:UsersStack.java [line: 17]
Error: cannot find symbol
symbol: method push(java.lang.Integer)
location: variable stack of type Stack
File: C:UsersStack.java [line: 21]
Error: cannot find symbol
symbol: method push(java.lang.Integer)
location: variable stack of type Stack
File: C:UsersStack.java [line: 25]
Error: cannot find symbol
symbol: method push(java.lang.Integer)
location: variable stack of type Stack
File: C:UsersStack.java [line: 29]
Error: cannot find symbol
symbol: method push(java.lang.Integer)
location: variable stack of type Stack
File: C:UsersStack.java [line: 33]
Error: cannot find symbol
symbol: method push(java.lang.Integer)
location: variable stack of type Stack
File: C:UsersStack.java [line: 41]
Error: cannot find symbol
symbol: method pop()
location: variable stack of type Stack
import java.util.*;
public class Stack {
public static void main(String args[]) {
int sum = 0;
Stack<Integer> stack = new Stack<Integer>();
System.out.println("stack: " + stack);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the second number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the third number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the first number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the first number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("stack: " + stack);
try {
for (int i = 0; i < 5; i++){
Integer number = (Integer) stack.pop();
sum += number;
}
} catch (EmptyStackException e) {
System.out.println("Stack is empty");
}
System.out.println("The sum of all the numbers is : "+sum);
}
}
Explanation / Answer
import java.util.*;
public class Stack_demo
{
public static void main(String args[])
{
int sum = 0;
Stack stack = new Stack();
System.out.println("stack: " + stack);
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the first number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the second number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the third number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the fourth number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("Enter the fifth number");
stack.push(new Integer(scanner.nextInt()));
System.out.println("stack: " + stack);
try
{
for (int i = 0; i < 5; i++)
{
Integer number = (Integer) stack.pop();
sum += number;
}
}
catch (EmptyStackException e)
{
System.out.println("Stack is empty");
}
System.out.println("The sum of all the numbers is : "+sum);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.