"java.util.NoSuchElementException: No line found" but i can't find the cause

3 weeks ago 31
ARTICLE AD BOX

For about a week I've been learning and practicing Java through the mooc.fi website and now, at the task where I'm supposed to write a program that calculates the average of positive numbers I got the first error that I can't seem to see the solution to get rid of it.

However I looked it up and all I've found seems to be about an issue with the scanner and about closing it? There was no information about "closing" the scanner yet at the stage of the course where I'm right now.

I can fully use it. With the scanner I insert random numbers, the program sums it together and then divides it by how many numbers I inserted. But every time I try to submit the program over TMC it gives me an error.

My written code is:

import java.util.Scanner; public class AverageOfPositiveNumbers { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int positive = 0; int num = 0; double avg = 0; while (true) { int input = Integer.valueOf(scanner.nextLine()); if (input > 0) { positive = positive + input; num = num + 1; } else if (input == 0 && num > 0) { avg = (double) positive / (double) num; System.out.println(avg); break; } else if (input == 0 && num == 0) { System.out.println("Cannot calculate the average"); } } } }

The detailed error message is:

Something unexpected happened. The public static void main(String[] args) method of 'class AverageOfPositiveNumbers' class has disappeared or your program crashed because of an exception. More info: java.util.NoSuchElementException: No line found Assert.java:88: org.junit.Assert.fail AverageOfPositiveNumbersTest.java:56: AverageOfPositiveNumbersTest.callMain AverageOfPositiveNumbersTest.java:39: AverageOfPositiveNumbersTest.test AverageOfPositiveNumbersTest.java:17: AverageOfPositiveNumbersTest.test1 NativeMethodAccessorImpl.java:-2: jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 NativeMethodAccessorImpl.java:62: jdk.internal.reflect.NativeMethodAccessorImpl.invoke DelegatingMethodAccessorImpl.java:43: jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke Method.java:566: java.lang.reflect.Method.invoke FrameworkMethod.java:47: org.junit.runners.model.FrameworkMethod$1.runReflectiveCall ReflectiveCallable.java:12: org.junit.internal.runners.model.ReflectiveCallable.run FrameworkMethod.java:44: org.junit.runners.model.FrameworkMethod.invokeExplosively InvokeMethod.java:17: org.junit.internal.runners.statements.InvokeMethod.evaluate FailOnTimeout.java:74: org.junit.internal.runners.statements.FailOnTimeout$StatementThread.run

Test code:

import fi.helsinki.cs.tmc.edutestutils.MockStdio; import fi.helsinki.cs.tmc.edutestutils.Points; import fi.helsinki.cs.tmc.edutestutils.ReflectionUtils; import java.lang.reflect.Method; import org.junit.*; import static org.junit.Assert.*; @Points("02-13") public class AverageOfPositiveNumbersTest { @Rule public MockStdio io = new MockStdio(); @Test(timeout = 1000) public void test1() { test("0\n", "nnot", "0", "1", "-1"); } @Test(timeout = 1000) public void test2() { test("1\n2\n0\n", "1.5", "0"); } @Test(timeout = 1000) public void test3() { test("-1\n3\n0\n", "3.0", "1"); } @Test(timeout = 1000) public void test4() { test("1\n1\n1\n0\n", "1.0", "0.3", "0.7"); } public void test(String input, String expected, String... notExpected) { int oldOut = io.getSysOut().length(); io.setSysIn(input); callMain(AverageOfPositiveNumbers.class); String out = io.getSysOut().substring(oldOut); assertTrue("When input was:\n" + input + ", the expected out put was:\n" + expected + "\nOutput was not found.", out.contains(expected)); for (String unexpected : notExpected) { assertFalse("When input was:\n" + input + ", output shouldn't contain:\n" + unexpected + "", out.contains(unexpected)); } } private void callMain(Class kl) { try { kl = ReflectionUtils.newInstanceOfClass(kl); String[] t = null; String x[] = new String[0]; Method m = ReflectionUtils.requireMethod(kl, "main", x.getClass()); ReflectionUtils.invokeMethod(Void.TYPE, m, null, (Object) x); } catch (Throwable e) { fail("Something unexpected happened. The public static void main(String[] args) method of '" + kl + "' class has disappeared \n" + "or your program crashed because of an exception. More info: " + e); } } }
Read Entire Article