Error while testing

Error while testing

por miki Alliel -
Número de respuestas: 4

Hello all,

i have written a program that check if the number is even or odd:

import java.util.Scanner;

public class q3 {

    public static void main(String[] args) {

        Scanner scn = new Scanner(System.in);

        int num,k=0;

        System.out.println("Enter Number");

        while (k==0){

        num=scn.nextInt();

        if (num%2!=0)

        System.out.println("your number is odd, enter another number");

        else k++;

        }

        System.out.println("your number is even");

           }

}


my test case are:

num=1; the number is odd.

num=2; the number is even.


the program run fine with Eclipse.

but when i try to test it in the coderunner question as a java program, i get this error:



waiting for your reply,

thank you very much.


En respuesta a miki Alliel

Re: Error while testing

por Richard Lobb -

Hi Miki

I'm not used to reading things right to left, but it looks to me like you've used the Java Program question type but without defining any standard input. With that question type, the program entered by the student is executed exactly as supplied for each test case. The standard input needs to be defined for each test case. Your program is trying to read from standard input using the Scanner class, but since there is no standard input supplied you get a NoSuchElement execution on calling sc.nextInt.

To make your example work for your first test case you should remove the Test code as it's not used and you should set the standard input to '1 2' (without the quotes). Furthermore the Expected should be

Enter number
your number is odd enter another number
your number is even

Note that standard input is not echoed to the output.

Richard

En respuesta a Richard Lobb

Re: Error while testing

por miki Alliel -
Thank you for your reply.

we are new to CodeRunner and thus our little knowledge.

this is my test case settings:


but still I get this error:



we appreciate your kind help,


En respuesta a miki Alliel

Re: Error while testing

por miki dream -
Can anyone help me with this? please
En respuesta a miki dream

Re: Error while testing

por Richard Lobb -
I think you're misunderstanding how the tests work. The program is executed once for each test. You want the program to read a number, print a message saying if it's even or odd, then read another number if the first one was odd. In that case both the input numbers must appear in the same test, not in separate tests. Please re-read my previous answer about how to define the standard input and the expected output.


Richard