Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
leimath opened this issue Nov 11, 2023 · 3 comments
Assignees

Comments

@leimath
Copy link

leimath commented Nov 11, 2023

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?

@Kadam-Tushar Kadam-Tushar self-assigned this Nov 14, 2023
@Kadam-Tushar
Copy link
Owner

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

@leimath
Copy link
Author

leimath commented Nov 14, 2023

I see. Thank you!

@leimath leimath closed this as completed Nov 14, 2023
@Kadam-Tushar
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants