1. How to avoid NZEC error in java? 2. Is this the correct way to take inputs for test cases

37 Views Asked by At

!(https://i.stack.imgur.com/UpHxf.png) When I run the code on my Eclipse IDE , it reads the input properly through the console but when I try to submit the code on SPOJ , the compiler throws NZEC error

I have come to the conclusion that the error shows up because of the way I'm trying to read the input

Line 1 of input is the number of test cases followed by the inputs. Eg:

2 --> No of test cases
123 --> Input number
131 --> Closest next palindrome
41
44

Please tell me the right way to accept inputs or some fix.

import java.io.*;
import java.io.BufferedReader;
import java.lang.*;

public class NextPalindrome_SPOJ {

    public static void main(String[] args) throws IOException {
        int t = Integer.valueOf((new BufferedReader(new InputStreamReader(System.in))).readLine());
        // BufferedReader r = new BufferedReader (new InputStreamReader (System.in));
        // String s;
        // while((s = r.readLine()) != null && !s.isEmpty()) --> alternate way to read
        // input
        while (t-- > 0) {
            long n = Long.valueOf((new BufferedReader(new InputStreamReader(System.in)).readLine()));
            if (n < 9)
                System.out.print(n + 1);
            else {
                while (true) {
                    n++;
                    if (String.valueOf(n).equals(String.valueOf((new StringBuffer(String.valueOf(n)).reverse()))))
                        break;

                }
                System.out.println(n);
            }
        }
    }
}

OUTPUT:

Exception in thread "main" java.lang.NumberFormatException: null
    at java.base/java.lang.Long.parseLong(Long.java:662)
    at java.base/java.lang.Long.valueOf(Long.java:1151)
    at Main.main(Main.java:11)
1

There are 1 best solutions below

1
Maurice Perry On BEST ANSWER

As it's name implies, a BufferedReader is buffered. You cannot use it as you do in that code. Apart from that, avoid throwing exceptions out of a main method. Also, use StringBuilders instead of StringBuffer whenever you can: it's much faster. Try something like that:

    try (Reader reader = new InputStreamReader(System.in);
            BufferedReader in = new BufferedReader(reader)) {
        int t = Integer.parseInt(in.readLine());
        while (t-- > 0) {
            long n = Long.parseLong(in.readLine());
            if (n < 9)
                System.out.print(n + 1);
            else {
                while (true) {
                    n++;
                    String s = Long.toString(n);
                    String r = new StringBuilder(s)
                            .reverse()
                            .toString();
                    if (s.equals(r))
                        break;

                }
                System.out.println(n);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(NextPalindrome_SPOJ .class.getName()).log(Level.SEVERE, null, ex);
    }