How to get rid of getting NZEC error while submitting solution in java?

To all who are geting NZEC, try using this format in java:

import java.io.*;
import java.util.*;

public class Main
{
	public static void main(String[] args) throws IOException
	{
		try{
			//Your Solve
		}catch(Exception e){
			return;
		}
	}
}
22 Likes

My code runs well in my laptop but gets Runtime Error (NZEC) while submitting it…
Why it is so ?

4 Likes

I have a code that is correct but giving NZEC when submitted. How can i post the code i wrote for discussing it with others?

please check your solution, sometimes it is just because of your solution is giving wrong answer, try all possible cases as you can. I faced it recently and have gone through all possible cases and got to know that one of test case was failing and after changing my code accordingly, codechef has accepted my solution:)

NZEC means “Non zero exit code”. Its essentially saying that your program ran into some error during execution. Mostly, it comes up when there is a Segmentation Fault.

The SegFault can be caused by many things, but experience says it is mainly through two causes:

(a) Infinite Recursion - or basically when you run out of stack memory.

(b) Incorrect Memory Access -
or whenever there is some weird stuff happening with memory allocation / access. C++ isn’t so friendly as Java, and it will not explicitly tell you that you have an “ArrayIndexOutOfBounds Exception”, but will instead try to use the “supposed” memory even if it is outside the block. This makes things a bit hard to debug.

If you’re accessing things far out for example.

Some sample codes that should give NZEC on Codechef/Spoj:

Example 1: DFS

void dfs(int u)
{
visited[u]++;
for(int v = 0; v < N; v++)
if(adjmat[u][v] == 1)
dfs(v);
}

3 Likes

did that exception thing but my laptop is showing perfectly right answer… in that case where should i start looking error in my code…

If you are submitting the code by copying from your IDE, make sure you select the code from import statements not from your package statement.
This will also give you NZEC error. Please check your submission.

import java.util.*;

public class Main{

public static final double BANK_CHARGES = 0.50;
public static void main(String[] args) {
	Scanner console = new Scanner (System.in);
	System.out.printf("\t\tInput : \n\t\t");
	int x = console.nextInt();
	double y = console.nextDouble();
	check(x,y);
	y=y-x-BANK_CHARGES;
	output(y);
}

public static void check(int x, double y){
	if (x%5!=0){
		output(y);
		System.out.printf("\n\t\tIncorrect Withdrawble Amount (not multiple of 5)\n\n");
		throw new IllegalArgumentException(	);
	}
	else if ((y-x-BANK_CHARGES)<0){
		output(y);
		System.out.printf("\n\t\tInsufficient Fund\n\n");	
		throw new IllegalArgumentException();
	}
}

public static void output(double y){
	System.out.printf("\n\t\tOutput :\n\t\t%.2f",y);
}

}

import java.util.*;
class Main {
public static void main (String arg[]) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int arr[] = new int[n];
for(int i=0;i<n;++i) {
arr[i] = scan.nextInt();
}
System.out.println(rec(arr,n));
}
public static int rec(int arr[],int n){
if(n==0){
return 0;
}
if(n==1){
return arr[0];
}
if(n==2) {
if(arr[0]<arr[1])
return arr[0];
else
return arr[1];
}
int mat[] = new int[n];
mat[0] = arr[0];
mat[1] = arr[1];
mat[2] = arr[2];
for(int i=3;i<n;++i) {
mat[i]=arr[i]+min(mat[i-1],mat[i-2],mat[i-3]);
}
return min(mat[n-1],mat[n-2],mat[n-3]);
}
public static int min(int a,int b,int c){
if(a<=b && a<=c){
return a;
}else if(c<=b && c<=a){
return c;
} else{
return b;
}
}
}

This code runs well in my laptop but gets Runtime error(NZEC) while submitting, please help.

1 Like

I DONT UNDERSTAND WTF IS WRONG IN MY CODE ITS PERFECTLY WORKING ON JDK 9
AND CODECHEF IS SAYING IT HAS NZEC RUNTIME ERROR
/*

  • To change this license header, choose License Headers in Project Properties.
  • To change this template file, choose Tools | Templates
  • and open the template in the editor.
    /
    import java.io.
    ;
    import java.util.*;

/**
*

  • @author Aashlesh Dhumane
    */
    public class Main {

    /**

    • @param args the command line arguments

    • @throws java.lang.Exception
      */
      public static void main(String[] args) throws Exception
      {
      // TODO code application logic here
      int n;
      int i,j,k,l;
      System.out.println(“enter the test cases”);
      String str;
      BufferedReader b = new BufferedReader (new InputStreamReader(System.in));
      str=b.readLine();
      n=Integer.parseInt(str);
      // for (i=0;i<n;i++)
      // {
      while (n>0)
      {
      str=b.readLine();
      i=Integer.parseInt(str);
      str=b.readLine();
      j=Integer.parseInt(str);
      str=b.readLine();
      k=Integer.parseInt(str);
      str=b.readLine();
      l=Integer.parseInt(str);

        if ((i==j && k==l) || (j==k && l==i) || (i==k && j==l) )
       {System.out.println("YES");
       }
        else 
       { System.out.println("NO");
       }
                   n--;     
      

      }

      }

}

Alright I don’t seem to have a high enough Karma or something to ask a separate question so I thought I’d post it here since the topic is related.

I keep getting an NZEC error at the spot I marked by a comment in my code when running it on the online IDE set to the problem CHEFCHR (I’m aware the contest is still going on which is why I omitted specific code to prevent illegal use):

/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Scanner sc = new Scanner(System.in);
        
        int cases = sc.nextInt(); //THE NZEC ERROR IS HAPPENING HERE
        
        for(int i = 0;i < cases;i++)
        {
            String s = sc.next();

            /* Omitted code for sake of confidentiality since the contest is still running */
        }
    }
}

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Codechef.main(Main.java:14)

When I run this code in my ide (NetBeans), I’m fine and everything works perfectly. When I run it on the online IDE with custom input, everything is fine as well. When I run it with the CHEFCHR input, I’m given an NZEC error. When I’ve been submitting, it’s been telling me my code gives the wrong answer.

Even if I run LITERALLY ONLY the code below, I get the same exact NZEC error:

import java.util.*;
import java.lang.*;
import java.io.*;

class Codechef
{
    public static void main (String[] args) throws java.lang.Exception
    {
        Scanner sc = new Scanner(System.in);
    
        int cases = sc.nextInt(); 
    }
}

Exception in thread "main" java.util.NoSuchElementException
at java.util.Scanner.throwFor(Scanner.java:862)
at java.util.Scanner.next(Scanner.java:1485)
at java.util.Scanner.nextInt(Scanner.java:2117)
at java.util.Scanner.nextInt(Scanner.java:2076)
at Codechef.main(Main.java:11)

What am I doing wrong if even just calling for scanner to read in an integer is giving me an error?

11 Likes

Further example of the absolute insanity going on right now:

A proven AC solution to a practice problem:
https://www.codechef.com/viewsolution/17177392

Me copying and pasting that EXACT same code into the online IDE and submitting the solution to that SAME EXACT practice problem:
https://www.codechef.com/viewsolution/17344717

Both programs are EXACTLY the same. No alterations. Both are answering the same question. Yet one is right and one is wrong. Plz help, I’m losing my marbles.

1 Like

I am facing the same problem. I have used Scanner class for input and i am getting the error in the exact same line. I have tried the try-catch block. The NZEC problem still persists. Please help.

package com.nec.hacker;

import java.util.Arrays;
import java.util.Scanner;

class SweetsHacker {

private static Scanner scan;

public static void main(String[] args) {

	scan = new Scanner(System.in);
	System.out.println("Number of Test Cases please ?");
	int testCases = scan.nextInt();
	scan.nextLine();

	int totalAmount = 0;
	int remainder = 0;
	int quoteint = 0;
	int sum = 0;

	while (testCases > 0) {
		System.out
				.println("Number of bank notes given by customer & the value of sweets");
		String[] bankNotesNoAndCostOfSweet = scan.nextLine().split(" ");
		System.out.println("Cost of "+bankNotesNoAndCostOfSweet[0]+" sweets ? " );
		String[] bankNotesValue = scan.nextLine().split(" ");

		int[] bankNotesValueIntArray = Arrays.asList(bankNotesValue)
				.stream().mapToInt(Integer::parseInt).toArray();
		java.util.Arrays.sort(bankNotesValueIntArray);
		for (int i = 0; i < bankNotesValue.length; i++) {
			totalAmount = totalAmount + Integer.valueOf(bankNotesValue[i]);
		}
		if (totalAmount > 0) {
			remainder = totalAmount
					% Integer
							.valueOf(bankNotesNoAndCostOfSweet[bankNotesNoAndCostOfSweet.length - 1]);
		}
		if (remainder > 0) {
			sum = totalAmount - Integer.valueOf(bankNotesValueIntArray[0]);
			if (sum
					/ Integer
							.valueOf(bankNotesNoAndCostOfSweet[bankNotesNoAndCostOfSweet.length - 1]) == totalAmount
					/ Integer
							.valueOf(bankNotesNoAndCostOfSweet[bankNotesNoAndCostOfSweet.length - 1])) {
				System.out.println("-1");
			} else {
				System.out.println("1");
			}
		}
		printQuoteint(quoteint, bankNotesNoAndCostOfSweet, remainder,
				bankNotesValue, totalAmount);
		// checkForSmallerRemainder(totalAmount,bankNotesValue,remainder);
		testCases--;
	}

}

private static void printQuoteint(int quoteint,
		String[] bankNotesNoAndCostOfSweet, int remainder,
		String[] bankNotesValue, int totalAmount) {
	// TODO Auto-generated method stub
	if (remainder == 0) {
		quoteint = totalAmount
				/ Integer
						.valueOf(bankNotesNoAndCostOfSweet[bankNotesNoAndCostOfSweet.length - 1]);
		System.out.println(quoteint);
	}

}

}

Getting NZEC error here also.Although works like a charm in my PC.

NZEC for no reason ?

https://www.codechef.com/viewsolution/20662453

NZEC might occur because sometimes your code must not be getting passed for all possible types of Test Cases. Please understand the problem statement and try to run your code with various scenarios. There is a possibility that the code might be failing for a particular Test case.
It has happened with me, as well. I found out that my code wasn’t working for a particular TC. I changed my code logic accordingly and NZEC error did’t occur.

Codechef accepted my solution.
So please design your code that handles as many varieties of Test cases as possible.

try to provide input like this

int y;
if(sc.hasNextInt) y=nextInt;

//with all data types
//sc is instance of scanner class

Try running the code with input if you are using java platform.It will not produce that error with input .Try it and let me know if it works.

but if it throws an exception then it will not execute the code…then u’ll get a WA…it is better if u check if your input format is correct…i.e while using bufferedReader and specially while using the readLine() function…also u should take care array out of bounds excep…the above format will only help to find where the error is occurring and that to only if u have the specific cases…as the example cases or many small cases may pass…:slight_smile:

1 Like

NO actually you see, it’s not like that! That’s the convention how you write it at the beginning at the function. It’s a built-in exception class. The way I coded is similar to something like below and both means the same, just different convention or writing practice-

public static void main(String[] args {
        try{
            //Your Solve
        }catch(IOException e){
            System.err.println(e.getMessage());
             return;
        }
}