+ 1

What is causing this exception and how can I fix it?

When I run this code in my IDE it work perfectly fine with no errors but when I export it to the SoloLearn code playground, I get this error: Exception in thread "main" java.util.NoSuchElementException: No line found at java.basejava.util.Scanner.nextLine(Scanner.java:1651) at Mathematics.main(Mathematics.java:91) I have no clue why this error is occuring and would be extremily greatful if someone could help me and suggest how I could fix this. https://code.sololearn.com/cn6fq711CfU7/?ref=app

28th Apr 2020, 4:07 PM
rafalzacher1
7 odpowiedzi
+ 11
Don't use 2 Scanner variables that use the same stream. Scanner scanner = new Scanner (System.in); int correctAnswer = scanner.nextInt(); scanner.nextLine(); // use after use of any next(), nextInt(), etc as they don't consume the remaining '\n' character. String equations = scanner.nextLine();
28th Apr 2020, 4:34 PM
ChaoticDawg
ChaoticDawg - avatar
+ 2
How you are giving the input..? According to your code, it need to give as 10 5*2 Or change code like Scanner scanner = new Scanner (System.in); int correctAnswer = scanner.nextInt(); String equations = scanner.nextLine(); Now you can give it in one line as 10 5*2 If second input contains only one word, then use next() method instead of nextLine().. Edit: Hoping that you know SoloLearn takes all inputs only once before start of execution...
29th Apr 2020, 8:17 AM
Jayakrishna 🇮🇳
+ 1
Thank you for the help, this solved my problem. Could you possible help me again. I've passed this program into the test and test 3 & 4 failed for some reason. I tried all the calculation and they work fine and also the indexing works fine but something is wrong and can't see the issue.
28th Apr 2020, 6:14 PM
rafalzacher1
0
I've written my program with your suggestions, thank you very much. I found a post on stackoverflow that explained a method that can be used to workout equations from strings really easily. So, the code is much more elegant now too. The porblem that was happeneing with the failed tests were that I've had two scanners and that I didint take it to account that I will also have to check for floating point numbers so the test 3 and fours where for floating points. https://code.sololearn.com/cn6fq711CfU7/?ref=app
29th Apr 2020, 11:08 AM
rafalzacher1
0
That is strange actually. I tried integer but 2 tests failed but when I tried doubles it worked perfectly fine
29th Apr 2020, 11:53 AM
rafalzacher1
0
Isn't it because Sololearn is doing matience
29th Apr 2020, 7:31 PM
Lomy Tv
Lomy Tv - avatar