Kadam-Tushar/Competitive-Programming-in-Java

How to use the template to take input where the input is supposed to be EOF terminated?

Closed this issue ยท 3 comments

I am using this template and I was trying to solve this https://open.kattis.com/problems/countingstars.
The problem is that it is expected that the input will end at EOF. Can it be done solely with the code in the template? Or do I have to make a new method to detect (e.g, like Scanner's .hasNext())?

I apologize if this is something trivial. I am new to java.

Edit: Also any tips on how to practice/in what sequence to do topics to be able to solve more difficult problems?

Hi @leimath,
I am glad that this repository is helping you.
There is a workaround that I use when input is supposed to be EOF terminated.
I used an infinite while loop which breaks when EOF is reached. But beware it is not advised to use infinite loops in your codes because it might have side-effects.
Here is the code snippet for taking EOF terminated input for the problem you mentioned in the issue.

void solve() throws Exception {
while(true){
    pn("reading again!");
    try{
    int n = ni();
    int m = ni();
    pn("read n,m "+n+" "+m);
    out.flush();
    char[][] mat = nm(n,m);
    pn("read matrix ");
    out.flush();
    }
    catch(InputMismatchException e){
        break ;
    }
}   
}

I see. Thank you!

Regarding your another query, its been a while that I have stopped doing CP so I might be unaware what people follow nowadays. During my time I used to follow CSES problemset for learning and live contests for practicing