1 / 5
Jul 2014

Now let me be very frank...I'm new to the world of programming and I've been recieving this NZEC run time error quite a lot...Almost in every problem I try..And I tell you its very frustating..Can anybody plz tell me how to fix this...i'm repeated getting NZEC in almost all codes I try frowning frowning frowning

Here's my solution to ADDREV:

import java.io.*;
class REV
{

public static void main(String args[])throws IOException
{
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
int x= Integer.parseInt(br.readLine());

while(x>0)
{x--;
String s=br.readLine();
String t= s.substring(0,s.indexOf(32));
String v=s.substring(s.indexOf(32)+1);
int c,m,i,a,b;
a=t.length();b=v.length();

if(a{c=a;
a=b;b=a;
s=v;
v=t;
t=s;
}
c=m=i=0;boolean flag=false;
a-=b;
while(b>0)
{b--;
c+=t.charAt(i)+v.charAt(i)-96;
m=c%10;
c/=10;
if(flag||m!=0){System.out.print(m);flag=true;}
i++;
}
while(a>0)
{a--;
c+=t.charAt(i)-48;
m=c%10;
c/=10;
if(flag||m!=0){System.out.print(m);flag=true;}
i++;
}
if(c!=0)System.out.print(c);
System.out.println();
}
}}

  • created

    Jul '14
  • last reply

    Sep '14
  • 4

    replies

  • 813

    views

  • 5

    users

  • 1

    link

Welcome to SPOJ!!!

Please use code tags while posting codes.

You are getting NZEC because of the way you are reading the input.

The best way to read the input in Java is Scanner(but too slow) BufferedReader are good.

while reading the input using buffered reader use string tokenizer or split(" ")
docs.oracle.com/javase/tutorial/ ... nning.html8
split separate the whole sting in small tokens separated by white space
Eg[bbone=java,733]import java.util.*;
import java.lang.*;
import java.io.*;

class Addrev
{
public static int rev(int a)
{
int r = 0,x = 0;
while ( a > 0)
{
r = r*10+a%10;
a /= 10;
}
return r;

}
public static void main (String[] args) throws java.lang.Exception
{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	int t,a,b,ans;
	t = Integer.parseInt(br.readLine());
	while(t-- > 0)
	{
		String [] w = br.readLine().split(" ");
		ans = rev(Integer.parseInt(w[0])) + rev(Integer.parseInt(w[1]));
		System.out.println(rev(ans));

	}
}

}
[/bbone]

23 days later

NZEC means non zero exit code best way to tackle this is by wrapping your code in try catch

try{
}catch(Exception e){
System.exit(0);
//or simply
return;
}
28 days later

Hi,
I am facing the same problem while submitting the solution for Prime generator. I ran the same code on ideone and it gives me perfect answer but it is not working on SPOJ please help. Here is my code:

import java.util.Scanner;

class Answers
{
public static void main(String[] args)
{
Scanner s = new Scanner(System.in);

    int num_of_test =  s.nextInt();

    while( num_of_test > 0)
    {
        int lower = s.nextInt();
        int upper = s.nextInt();
        int bound = (int)Math.sqrt(upper);

        int arr[] = new int[upper + 1];
        arr[0] = -1;
        int i;

        for(i = 1; i <= upper; i++)
            arr[i] = i;
        for(i = 2; i<=bound; i++)
            crossOut(upper, i, arr);
        for(i=lower; i<=upper; i++)
            if(arr[i]!= 1)
              System.out.println(i);
        System.out.println();
        num_of_test--;    
    }

}

public static void crossOut(int u, int i, int[] a)
{
if(a[i]==1)
return;
for(int l = 2*i; l<=u; l= l+i)
a[l]=1;
return;
}
}

Please use code tags when posting code. Please keep your posts on topic, and preferably problem specific.

Suggested Topics

Want to read more? Browse other topics in JAVA based languages or view latest topics.