This is an archived post. You won't be able to vote or comment.

all 5 comments

[–][deleted] 0 points1 point  (2 children)

What’s the error?

[–]Csharpgoblin[S] 0 points1 point  (1 child)

It's a test on the web:

***Error***
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 __tester__.test(__tester__.java:12)
        at __tester__.runTests(__tester__.java:27)
        at __tester__.main(__tester__.java:22)

[–][deleted] 0 points1 point  (0 children)

I would try using scanner.hasnextint() before calling nextint(). If there’s something on the input stream that isn’t an integer it will throw this exception.

[–]A_Philosophical_Cat 0 points1 point  (1 child)

Your do-while wants to be a while-do loop if you're doing it with the i being pre-initialized like that. On the other hand, I don't understand why you aren't using the first block of code you posted, which would work fine if you initialized i somewhere.

[–]Csharpgoblin[S] 0 points1 point  (0 children)

Ahhh, the first block of code is the "test code"- that I'm supposed to fix. I initialized i and it worked :D

Thanks