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

Thanks it helped me alot

for _ in range(int(input())):
    n=int(input())
    l=set(map(int,input().strip().split()))
    print(len(l))

how to remove NZEC in this?

Input format
1
3
1 2 3

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

/* 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)
{

	Scanner scan = new Scanner(System.in);
	int withdraw = scan.nextInt();
	
	int balance = scan.nextInt();
	
	if(withdraw<balance && withdraw % 5 ==0)
	{
        System.out.println(balance-withdraw-0.50);
	}
	else if (withdraw<balance && withdraw % 5 !=0) 
	{
	    System.out.println(balance);
	}
	else
	{
	    System.out.println(balance);
	}
}

}

use below code to get rid from noSuchElementException…

if(sc.hasNextInt()){
int cases = sc.nextInt();
}

2 Likes

try catch is not working bro
When i use try catch as you said it gives wrong answer
here is my code

import java.io.*;

class attendence {
    static class Reader {
        final private int BUFFER_SIZE = 1 << 16;
        private DataInputStream din;
        private byte[] buffer;
        private int bufferPointer, bytesRead;

        public Reader() {
            din = new DataInputStream(System.in);
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public Reader(String file_name) throws IOException {
            din = new DataInputStream(new FileInputStream(file_name));
            buffer = new byte[BUFFER_SIZE];
            bufferPointer = bytesRead = 0;
        }

        public String readLine() throws IOException {
            byte[] buf = new byte[64]; // line length
            int cnt = 0, c;
            while ((c = read()) != -1) {
                if (c == '\n')
                    break;
                buf[cnt++] = (byte) c;
            }
            return new String(buf, 0, cnt);
        }

        public int nextInt() throws IOException {
            int ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public long nextLong() throws IOException {
            long ret = 0;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (neg)
                return -ret;
            return ret;
        }

        public double nextDouble() throws IOException {
            double ret = 0, div = 1;
            byte c = read();
            while (c <= ' ')
                c = read();
            boolean neg = (c == '-');
            if (neg)
                c = read();
            do {
                ret = ret * 10 + c - '0';
            } while ((c = read()) >= '0' && c <= '9');
            if (c == '.') {
                while ((c = read()) >= '0' && c <= '9') {
                    ret += (c - '0') / (div *= 10);
                }
            }
            if (neg)
                return -ret;
            return ret;
        }

        private void fillBuffer() throws IOException {
            bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
            if (bytesRead == -1)
                buffer[0] = -1;
        }

        private byte read() throws IOException {
            if (bufferPointer == bytesRead)
                fillBuffer();
            return buffer[bufferPointer++];
        }

        public void close() throws IOException {
            if (din == null)
                return;
            din.close();
        }
    }

    static Reader sc = new Reader();
    static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String args[]) throws IOException {
        int t = inputInt();
        while (t-- > 0) {
            int N = inputInt();
            String s = inputString();
            // StringBuilder sb = new StringBuilder(inputString());
            double possible_attendence = ((120 - N) / 120) * 100;
            if (possible_attendence >= 75) {
                println("YES");
            } else {
                int present = 0;
                for (int i = 0; i < N; i++) {
                    if (s.charAt(i) == '1') {
                        present++;
                    }
                }
                possible_attendence = (present + (120 - N) / 120) * 100;
                if (possible_attendence >= 75) {
                    println("YES");
                } else {
                    println("NO");
                }
            }

        }
        bw.flush();
        bw.close();
    }

    public static int inputInt() throws IOException {
        return sc.nextInt();
    }

    public static long inputLong() throws IOException {
        return sc.nextLong();
    }

    public static double inputDouble() throws IOException {
        return sc.nextDouble();
    }

    public static String inputString() throws IOException {
        return sc.readLine();
    }

    public static void print(String a) throws IOException {
        bw.write(a);
    }

    public static void printSp(String a) throws IOException {
        bw.write(a + " ");
    }

    public static void println(String a) throws IOException {
        bw.write(a + "\n");
    }

    public static void println(int a) throws IOException {
        bw.write(a + "\n");
    }
}

Very weird situation!

I kept getting this error when I was using nextLine() method to take the string input. Then I used next() method and the error disappeared.

1 Like

thanks man

I also faced the same problem. I found out that if multiple scanners are instantiated , then the scanners take the whole input as the input for one single scanner.

Hope this helps!!!

now it gives me wrong answer even after successful execution…while it runs fine on bluej

thank you! it’s working…

/* 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 scanner = new Scanner(System.in);
	    int t = scanner.nextInt();
	    System.out.println(t);
	}
}
    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)

Help me out here guys :frowning:

Are you trying to “Run” without Providing “Custom Input”?

Ohh … Yes now i understand… Thank you bro

1 Like

this Code works well in my Laptop but giving runtime error(NZEC) while submitting solution. PLEASE HELP!!!
here is my code---->

import java.util.*;

public class Devu_Charu_Game {

public static void main(String args[]) {
 
       Scanner sc = new Scanner(System.in);
    if (sc.hasNext()) {
        int N = sc.nextInt();
        int M = sc.nextInt();
        int a[] = new int[N];
        for (int i = 0; i < N; i++) {
            a[i] = sc.nextInt();
        }

        int x[] = Get(a, N);
        for (int j = 0; j < M; j++) {
            char C = sc.next().charAt(0);
            int Q = sc.nextInt();
            char W = sc.next().charAt(0);
            int temp = 0;
            if (C == '>') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] > Q) {
                        temp++;
                    }
                }
            }

            if (C == '<') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] < Q) {
                        temp++;
                    }
                }
            }

            if (C == '=') {
                for (int i = 0; i < x.length; i++) {
                    if (x[i] == Q) {
                        temp++;
                    }
                }
            }
            if (W == 'D') {
                if (temp % 2 == 0) {
                    System.out.println("C");
                } else {
                    System.out.println(W);
                }
            }
            if (W == 'C') {
                if (temp % 2 == 0) {
                    System.out.println("D");
                } else {
                    System.out.println(W);
                }
            }

        }
    }


}

public static int[] Get(int a[], int N) {
    int na[] = new int[(N * (N + 1) / 2)];
    int x = 0;
    for (int i = 0; i < N; i++) {
        int max = a[i];
        for (int j = i; j < N; j++) {
            if (max >= a[j]) {
                na[x] = max;
                x++;
            }
            if (a[i] < a[j]) {
                max = a[j];
                na[x] = max;
                x++;
            }
        }
    }
   
    return na;
}

}

N can be as large as 10^6.

2 Likes

use try and catch block.

follow this part by

throws java.lang.Exception

try using this before you take any input:

if (!sc.hasNextInt())
System.exit(0);

you may also try:

throws java.lang.Exception

thanks buddy!!
but i am unable to use for(long i=0;i<N;i++) with array.
index value of array is long and stored values is int.
its showing lossy conversion from long to int for—> (x[i]<Q)

its like I have to use different data structure? :sweat_smile:

I have tried this!!
its giving ArrayIndexOutOfBoundException.

can you send the sepcifc solution link? it’ll help in assessing the problem.