+ 1

Java exercise about Stack, I'm stuck, could you help me?

I'm doing exercise about Stack implementing undo and redo methods, this exercise is in another app to learn Java. There are some videos that explain, but is not enough to solve this exercise. I tried many ways, but nothing happens to Stack st and Stack stredo(In this case when I comment in Main the local where was getting error[lines 51 and 53]). I don't know if methods where I have done if are correct(only methods I edited, main method is not changed). Sorry my bad English, I hope you understand. TODO: are the hints inside method https://code.sololearn.com/cA0a2a2a4a15/?ref=app

28th Mar 2021, 6:06 PM
Ron
10 Answers
+ 3
Ron Next You have to define " String item" in the method.. You are using "item" within the method. If you remove String item from argument then it becomes undefined so define it in method. Remove as argument. String item = st.pop(); similarly in redo method
28th Mar 2021, 7:45 PM
Jayakrishna 🇮🇳
+ 5
line 18 and line 27, you set a parameter when defining undo() and redo(), so when you call them at line 51 and 53, a String argument is expected. Revise line 18 and line 27, and it should work.
28th Mar 2021, 6:22 PM
Gordon
Gordon - avatar
+ 4
public static void add(String item){ This line is correct, because you need to pass a string to add as new item. However, these two are wrong: public static String undo(String item){ public static String redo(String item){ Because you don't need to pass string into undo() and redo() See? After the changes, add data type when declaring the variable.
28th Mar 2021, 6:49 PM
Gordon
Gordon - avatar
+ 3
The error message says: required: String found: no arguments Do you see this?
28th Mar 2021, 6:46 PM
Gordon
Gordon - avatar
+ 2
Firstly, let's look at the error message. Please copy it to here.
28th Mar 2021, 6:44 PM
Gordon
Gordon - avatar
+ 2
You are using while loop which stop only when you input 4. So how many values you are giving.. what are inputs? Error saying 'no input'.. ex: input:Ron 1 a 1 b 2 3 4 it working fine for above sample input.. To be clear, display stack after adding item..
28th Mar 2021, 8:27 PM
Jayakrishna 🇮🇳
+ 2
thanks Gordon and Jayakrishna🇮🇳 for help me, I found a solution.
28th Mar 2021, 9:13 PM
Ron
+ 1
@Gordon sorry, but I have no idea what to change there
28th Mar 2021, 6:38 PM
Ron
+ 1
But if I change: public static String undo(String item){ to: public static String undo(){ or: public static void undo(){ I'm getting error.
28th Mar 2021, 6:55 PM
Ron
+ 1
thanks Jayakrishna. 2 errors corrected. My logic inside that methods are correct? It works, but some errors appeared now after insert inputs: Exception in thread "main" java.util.NoSuchElementException at java.base/java.util.Scanner.throwFor(Scanner.java:937) at java.base/java.util.Scanner.next(Scanner.java:1594) at java.base/java.util.Scanner.nextInt(Scanner.java:2258) at java.base/java.util.Scanner.nextInt(Scanner.java:2212) at Main3.main(Main3.java:39)
28th Mar 2021, 8:18 PM
Ron