Bug in calculator command. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Bug in calculator command.

So I have been tasked to create a custom notepad program, I decided to add a few features including a calculator. In order to access these feature my user would input a command to access them. The way the program would work is first, It welcomes the user to the program, then it tells them to enter their first and last name. It will then teach them the commands, where if they input nameinfo, the program returns their name. The commands are all based in separate classes which use polymorphism to extend them to each other and extend to the main Notepad class. The calculator command is extended to the NameRecallCMD class (The nameinfo command). The way the calculator command works is: A user inputs calculator to acces it. Then they type in 2 numbers, after that they input an operation they want for the result. So if they want to do addition on the numbers they input: calculator 1 2 sum // this is to perform an addition operation. Output: 3 The problem is after they input sum. The program starts throwing errors. https://code.sololearn.com/comYWVwhLKGV

1st Aug 2018, 5:42 PM
C0D3X
C0D3X - avatar
9 Answers
+ 1
Sorry that I'm late. In line 95, userInput.nextLine() seems to return immediately, because you didn't use nextLine before, so you can fix it by using userInput.next(). In the following if statement, you are comparing Strings using ==, which you should never do in Java, because it only compares the reference, not what's actually inside the String. To do it right, you have to use the .equals() function, for example result.equals(resultSum).
6th Aug 2018, 5:47 PM
Moritz Vogel
Moritz Vogel - avatar
0
Please post your code, so we can help you
4th Aug 2018, 5:05 PM
Moritz Vogel
Moritz Vogel - avatar
0
https://code.sololearn.com/comYWVwhLKGV This is my program Thanks!
4th Aug 2018, 6:38 PM
C0D3X
C0D3X - avatar
0
@Moritz Vogel excuse me sir, where you able to find the bug? I have had no luck so far, I don't want to be a bother though, thank you!
6th Aug 2018, 5:16 PM
C0D3X
C0D3X - avatar
0
@Moritz Vogel, it is still giving me the same result. On the bright side I learned a new function! However, it takes me to the main class saying that the error is located at line 50 where I call the cmd2 method.
7th Aug 2018, 2:34 AM
C0D3X
C0D3X - avatar
0
You can't run this code in the code playground for some reason. To run it, you have to copy it into an ide and put the classes into separate files.
7th Aug 2018, 12:45 PM
Moritz Vogel
Moritz Vogel - avatar
0
Moritz Vogel That is what I thought and I had put the files into eclipse. But Eclipse is giving me the same result. Did you compile the code and see what line of code it was throwing errors at?
7th Aug 2018, 2:22 PM
C0D3X
C0D3X - avatar
0
That's strange. I tried it in IntelliJ IDEA and it ran fine
7th Aug 2018, 2:58 PM
Moritz Vogel
Moritz Vogel - avatar
0
ok let me see if I can run it in that IDE and i will get back to you. but im gone for a week soo thanks for the help mate.
7th Aug 2018, 3:00 PM
C0D3X
C0D3X - avatar