0

What's the problem with this code? The output is empty. Can anyone help?

public class MyStack { ArrayList<Integer> stack; MyStack(){ stack = new ArrayList<>(); } public void reverseEven() { System.out.print("\nReverse Even: "); for(int i : stack) { if(i % 2 == 0) System.out.printf("%d", i); } } public void reverseOdd() { System.out.print("\nReverse Odd: "); for(int i : stack) { if(i % 2 == 1) System.out.printf("%d", i); } } } public class Main { static MyStack myStack; public static void main(String[] args) { boolean isRepeat = true; int size = 0, choice = 0, num; Scanner console = new Scanner(System.in); System.out.print("Enter Size of Array: "); size = console.nextInt(); System.out.print("Input Numbers: "); num = console.nextInt(); myStack = new MyStack(); while (isRepeat) { System.out.println("\n\t-----TRANSACTION OPTIONS-----"); System.out.println("\t[1]Reverse Even"); System.out.println("\t[2]Reverse Odd"); System.out.println("\t[3]Exit"); System.out.print("\nEnter Choice: "); choice = console.nextInt(); switch(choice) { case 1: myStack.reverseEven(); break; case 2: myStack.reverseOdd(); break; case 3: System.out.println("Thank you"); isRepeat = false; break; default: System.out.println("Invalid Option! Try Again."); break; } } } } Enter Size of Array: 5 Input Numbers: 12345 -----TRANSACTION OPTIONS----- [1]Reverse Even [2]Reverse Odd [3]Exit Enter Choice: 1 Reverse Even:

24th Nov 2021, 3:32 PM
Sheng
5 Answers
+ 4
In code one you have to import some packages like Arraylist then try Check in the second code after class name you have written static MyStack my stack what you are trying to implement here then once check your input statement where u have written scanner And what is myStack =new myStack () it showing error here ebecz the things which u trying to implement which u have written previously after class name which is not defined properly
24th Nov 2021, 3:48 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Important packages which are required like ArrayList and in a single file you can't make two public classes
24th Nov 2021, 3:40 PM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
Shenna Justine Aviles Before you work with Scanner I sugesst you to test your MyStack class with a fixed example. Just create an array in your main and then think about how your myStack object receives this array. I don't see a method for it and you don't have a constructor. For me it looks like your ArrayList stays empty. If you tested all methods in your class and it works fine then start to implement a Scanner for user input.
24th Nov 2021, 3:52 PM
Denise Roßberg
Denise Roßberg - avatar
+ 1
♨️A.S. Raghuvanshi♨️ it's in different file though
24th Nov 2021, 3:40 PM
Sheng
0
♨️A.S. Raghuvanshi♨️ maybe you can help me improve it? This is the sample though Enter # on input: 5 (note: # of input may vary) Example Input: 1 2 3 4 5 Menu: [1]Reverse Even [2]Reverse Odd [3]Exit If choice == 1 Output: 4 2 if Choice == 2 Output: 5 3 1
24th Nov 2021, 4:30 PM
Sheng