I am using a fast I/O class which works on other platforms such as Codeforces, GFG, etc. but it is not working on Codechef. Can anybody point out the issue in this?
import java.io.*;
import java.util.*;
class MainClass{
public static void task(InputReader in){
int test=in.nextInt();//ISSUE
/*
SOME CODE
*/
System.out.println(test);
}
public static void main(String[] args)throws IOException {
try{
InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);
task(in);
// out.close();
}
catch(NumberFormatException e){
System.out.println(e);
}
}
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
In this code I have an issue in int test=in.nextInt();
Example:
Input: 5
Output:5
The above is the expected output but on codechef IDE, I get the following exception. Please help!
Runtime error: NZEC
Error
`Exception in thread "main" java.util.InputMismatchException
at MainClass$InputReader.read(Main.java:35)
at MainClass$InputReader.nextInt(Main.java:53)
at MainClass.task(Main.java:5)
at MainClass.main(Main.java:17)`
Note: This template works perfectly fine on other platforms but does not work on Codechef.
At the time of writing, your code does not compile in the Codechef IDE, so we can’t reproduce/ debug the problem:
Main.java:3: error: class MainClass is public, should be declared in a file named MainClass.java
public class MainClass{
^
Main.java:18: error: cannot find symbol
out.close();
^
symbol: variable out
location: class MainClass
2 errors
import java.io.*;
import java.util.*;
class MainClass{
public static void task(InputReader in){
int test=in.nextInt();//ISSUE
/*
SOME CODE
*/
System.out.println(test);
}
public static void main(String[] args)throws IOException {
try{
InputStream inputStream = System.in;
InputReader in = new InputReader(inputStream);
task(in);
// out.close();
}
catch(NumberFormatException e){
System.out.println(e);
}
}
public static class InputReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
Thanks, I guess. As the contest is still running, I cannot share the whole code but I am somehow getting the exception using this template. Still, thanks a lot!