【问题标题】:CODECHEF: Runtime Error (NZEC)CODECHEF:运行时错误 (NZEC)
【发布时间】:2016-03-16 21:24:09
【问题描述】:

我尝试了 codechef 的一个问题,并在 java 上编写了我的代码,该代码在我的笔记本电脑上的 eclipse 上完美运行。但每次我尝试提交代码时,它都会给我这个 NZEC 错误。 谁能告诉我为什么在执行此代码时出现非零退出代码错误(NZEC)。 这段代码的问题:https://www.codechef.com/problems/STRPALIN

    import java.util.*;
import java.io.*;
public class Palindrome {

    public boolean check() throws IOException{
        String A;
        String B;
        BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
        A=inp.readLine();
        B=inp.readLine();
        for(int i=0;i<A.length();i++)
        {
            for(int j=0;j<B.length();j++)
            {
                if(A.charAt(i)==B.charAt(j))
                    return true;
            }
        }
        return false;
    }


    public static void main(String[] args)throws NumberFormatException, IOException {
        Palindrome M = new Palindrome();
        boolean[] array = new boolean[10];
        BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
        int T = Integer.parseInt(in.readLine());
        for(int i=0;i<T;i++)
        {
            array[i]=M.check();
        }
        for(int j=0;j<T;j++){
            if(array[j])
                System.out.println("Yes");
            else
                System.out.println("No");

        }
    }
}

【问题讨论】:

  • 如果你使用System.exit(0) 作为你执行的最后一行会发生什么?
  • 总是把你的代码放在trycatchblock中。

标签: java runtime-error


【解决方案1】:

您的代码的问题是,在从用户那里获取String A 和 B 的输入时,readline() 方法返回 null,当您尝试访问 String A 或 B 时,NullPointerException被抛出。因此,非零退出代码。

现在,readline() 方法返回了null 值,因为您创建了两次BufferedReader 对象,导致内存泄漏。

参考此链接:readline() returns null in Java

【讨论】:

    【解决方案2】:

    关于 NZEC 错误:

    import java.util.*;
    import java.io.*;
     class Codechef {
    
        public boolean check() throws IOException{
            String A;
            String B;
            BufferedReader inp = new BufferedReader (new InputStreamReader(System.in));
            A=inp.readLine();
            B=inp.readLine();
            for(int i=0;i<A.length();i++)
            {
                for(int j=0;j<B.length();j++)
                {
                    if(A.charAt(i)==B.charAt(j))
                        return true;
                }
            }
            return false;
        }
    
    
        public static void main(String[] args)throws NumberFormatException, IOException {
            try {
                Codechef M = new Codechef();
            boolean[] array = new boolean[10];
            BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
            int T = Integer.parseInt(in.readLine());
            for(int i=0;i<T;i++)
            {
                array[i]=M.check();
            }
            for(int j=0;j<T;j++){
                if(array[j])
                    System.out.println("Yes");
                else
                    System.out.println("No");
    
            }
            } catch(Exception e) {
            } finally {
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多