Java fast i/o issue with codechef

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

Please paste code that

  1. Compiles; and
  2. Illustrates the problem you are having.
1 Like

I have removed the issue. Please have a look!

1 Like

Thankyou :slight_smile: With the current code in your post:

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;
        }
	}
}

I get the output 5, as expected:

This is using JAVA (HotSpot 8u112).

1 Like

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!:slight_smile:

1 Like

No problem. Just to confirm: you aren’t trying to “Run” without providing Custom Input, are you?

2 Likes

Woah!! You got it. That was the issue.
I thought it is used to check sample cases like on other platforms!
Thanks a lot! I am such an idiot.

Practically everyone makes this mistake :slight_smile: I would have suggested it sooner, but your original post had:

which I took to mean that you’d tried it with 5 as the Custom Input, but still got the Exception.

2 Likes

Well I was running it on other IDE, and when I was going to submit it on codechef, I hit run and got this error.