29 / 83
Apr 2007

SPOJ is not accepting my code? cry

Both solutions mentioned above also give wrong answer! open_mouth

Why does SPOJ says that this code gives wrong answer? Any one who solved this in SPOJ please help.

they say, it will be 1 or 2 digits only. they won't test 421 on the code. so chill smile

1 year later

Hello,
does anybody know why my code is not accepted? I get "runtime error (NZEC)". Earlier when I additionally surrounded the code-line "x = eingabe()" with a try-catch block in order to catch a NumberFormatException from the parseInt()-method i even got "time limit exceeded"... On my machine it works correctly. Here is my code:

import java.util.Iterator;
import java.util.LinkedList;
public class Main
{
	public static void main(String[] args)
	{
		int x;
		LinkedList<Integer> l = new LinkedList<Integer>();
		while(true)
		{
			x = eingabe();
			if(x != 42)
			{
				l.add(new Integer(x));
			}
			else
			{
				break;
			}
		}
		for(Iterator<Integer> i = l.iterator();i.hasNext();)
		{
			System.out.println(i.next());
		}
      System.exit(0);
	}
	static int eingabe()
	{
		String s = "";
		try
		{
			s = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)).readLine();
		}
		catch (java.io.IOException e)
		{
		}
		return java.lang.Integer.parseInt(s);
	}	
}
16 days later

No, the solution is correct, since 420 is an invalid input stuck_out_tongue Read the problem statement.

4 years later

Why my following code get runtime error (NZEC)?
Help!!!
5293653 2011-06-25 11:17:31 Life, the Universe, and Everything runtime error (NZEC)edit run 0.28 177M JAVA

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.*;
import java.util.*;
public class Main
{
	private void solve() throws IOException
	{
		for (; ; )
		{
			int N = nextInt();
			if (N == 42)
				break;
			writer.println(N);
		}
	}
	public static void main(String arg[])
	{
		new Main().run();
	}
	BufferedReader reader;
	StringTokenizer tokenizer;
	PrintWriter writer;
	public void run()
	{
		try
		{
			reader = new BufferedReader(new InputStreamReader(System.in));
			tokenizer = null;
			writer = new PrintWriter(System.out);
			solve();
			reader.close();
			writer.close();
		}
		catch (Exception e)
		{
			e.printStackTrace();
			System.exit(1);
		}
	}
	int nextInt() throws IOException
	{
		return Integer.parseInt(nextToken());
	}
	long nextLong() throws IOException
	{
		return Long.parseLong(nextToken());
	}
	double nextDouble() throws IOException
	{
		return Double.parseDouble(nextToken());
	}
	String nextToken() throws IOException
	{
		while (tokenizer == null || !tokenizer.hasMoreTokens())
		{
			tokenizer = new StringTokenizer(reader.readLine());
		}
		return tokenizer.nextToken();
	}
}
2 months later

I'm getting a NZEC error in code..plz check and reply.
import java.util.*;
public class Main {

public static void main(String args[])
{
	Scanner in=new Scanner(System.in);
	System.out.println("enter the numbers: ");

	int n=0;int i=0;
	int arr[]=new int[10];

	while((n=in.nextInt())!=0)
	{arr[i]=n;	
		i++;

	}
	int j=0;
	while(arr[j]!=42)
	{System.out.println(arr[j]);
	j++;

}


}

}

5 months later

public class Main
{
  public static void main (String[] args) throws java.lang.Exception
  {
     java.io.BufferedReader r = new java.io.BufferedReader (new java.io.InputStreamReader (System.in));
     String s;
     while (!(s=r.readLine()).startsWith("42")) System.out.println(s);
  }
}

I submitted this code and it got accepted... This is not my code though :mrgreen: .... but this code takes any input and print it out as it is.... Then why was it given in the problem that the number should be two digit or one digit integer only...

I made a different code it was taking all input but printing out only those which are integers of one or two digit... I think that is how it should work... But that code was not accepted... it gives wrong output it seems.. open_mouth

Please post the code that you are having difficulty with. (using code tags, see your post that I edited)

The code that you submit does not have to verify that the input follows the specification. The specification determines what sort of input will be given.

You can rest assured that numbers like 122 and 123455730324903242342393276378234123419 and 1.234 will not occur in the input.

6 months later

Why is my following code getting "wrong answer" in SPOJ.
It is working fine in my IDE.
Please HELP !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

import java.io.*;
public class Main
{
    /**
 *  This class solves problem 1 classical from SPOJ
 *  @author T
 *  @since 6-Sep-2011
 */
public static void main(String[] args) throws IOException
{
	BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	while(true)
	{
		System.out.println("Input:");
		String s = br.readLine();
		if(s.equals("42"))
			break;
		else
			System.out.println("Output:" + s);
	}

}
}

         System.out.println("Input:");

Everything that you output is considered in the result. This alone will give you wrong answer.

2 years later

Anybody knows why I keep getting a NZEC error when I try to submit?

I run it in my terminal as following
javac meaningoflife3.java
java meaningoflife3 test.txt

I think it might be because of the text file?

import java.io.*;
public class meaningoflife3 {
    public static void main(String[] args) throws IOException {
	// TODO Auto-generated method stub
	
	File inFile = null;
	
	if (0 < args.length) {
		inFile = new File(args[0]);	
		}
	try {
		String num;
		
	BufferedReader read = new BufferedReader(new FileReader(inFile));
	while ((num = read.readLine()) != null ){
		if ( num.equals("42"))
			break;
		else
			System.out.println(num);
		}
	}
	catch (IOException e){
		e.printStackTrace();
		}	
	}
	
	
}

Hope you guys can help me identify my error!

amenocal

11 days later

I have adapted a solution that seemed fine to me that was posted several years ago. When I tested it in an IDE I noticed that the PrintWriter caused the program to store the outputs until the end command "42" was reached. I then changed writer.println(s) to System.out.println(s) and indeed now the input was also returned as output immediately after pressing Enter. However, I still got a runtime error. Then, I changed int N = nextInt(); to String s = nextToken();. Still a runtime error. However, I now have found out that for some reason I cannot input strings, even though the nextToken() method returns tokenizer.nextToken().

Can anybody explain why the code below results in an NZEC runtime error? Keep in mind that the outputs are displayed after every input (which is commonly not the case for the NZEC runtime errors with this problem). I also don't know why my outputs are supposedly displayed "properly", as I don't see the difference between writer.println() and System.out.println(). Then I also wonder why I get an error whenever I input a string that is strictly not also a numeric value.

Any help would be appreciated! wink

/*
 * 
 * Date: 01-10-2014
 * Problem Statement link: http://www.spoj.com/problems/TEST/
 * Main site of P.S.: http://www.spoj.com/
 * 
 * Task name: TEST
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
// import java.math.*;
import java.util.*;
public class Main
{
   private void solve() throws IOException
   {
      for (; ; )
      {
         String s = nextToken();
         if (Integer.parseInt(s) == 42)
            break;
         System.out.println(s);
      }
   }
   public static void main(String arg[])
   {
      new Main().run();
   }
   BufferedReader reader;
   StringTokenizer tokenizer;
   PrintWriter writer;
   public void run()
   {
      try
      {
         reader = new BufferedReader(new InputStreamReader(System.in));
         tokenizer = null;
         writer = new PrintWriter(System.out);
         solve();
         reader.close();
         writer.close();
         // System.exit(0); // MUST INCLUDE THIS; VITAL FOR SUBMISSION TO SPOJ
      }
      catch (Exception e)
      {
         e.printStackTrace();
         System.exit(1);
      }
   }
   int nextInt() throws IOException
   {
      return Integer.parseInt(nextToken());
   }
   long nextLong() throws IOException
   {
      return Long.parseLong(nextToken());
   }
   double nextDouble() throws IOException
   {
      return Double.parseDouble(nextToken());
   }
   String nextToken() throws IOException
   {
      while (tokenizer == null || !tokenizer.hasMoreTokens())
      {
         tokenizer = new StringTokenizer(reader.readLine());
      }
      return tokenizer.nextToken();
   }
}

The behaviour you're seeing of output being printed immediately after input is normal.

The console is complicated in this regard because it both receives input from Sydtem.in and prints whatever arrives on System.out. As long as what you write to System.out is correct then that is ok.

A common practice is to redirect input and output to files for local testing so that you are not dealing with the console.