Read input data to EOF in URI Online Judge

Asked

Viewed 9,616 times

4

Hello, I’m trying to solve the following problem below:

inserir a descrição da imagem aqui

However, I am having trouble putting this EOF condition in java. Here on stack I found attempts solutions, but they are related to using files, and for my problem I’m not using a file.

For example:

while (fileReader.nextLine() != null) {
    String line = fileReader.nextLine();
    System.out.println(line);
}  

But this is with the use of file, which is not my case.

My problem is to represent this EOF in java. It follows the part of my code that I am in trouble:

public class Main {      
public static void main(String[] args) {        

    while(?){   //Condição de parada EOF
      ... código ...
    }

If I remember correctly, in C I could do something like...

while (number != EOF);

But how can I do it in java?


I solved the problem in C, stay like this:

int main(){
int N = 0;
while(scanf("%d",&N) != EOF){
... código ...
}

But I would like to solve in java, so far I’m no solution, some hint?

  • the stop condition would not be the input number you indicated? 4 or 7 for example? In this case you could put a counter inside the while that increases until you reach the number, and when the number is greater or equal to the stop condition, it stops while. int counter = 0; while(fieldNumber > counter){ counter ++; }

  • @Geferson, no, 4 and 7 represent the dimension of the matrix. From what I understood of the problem my program should receive several entries, like: 4 7 8 10 50 ... And the stopping criterion is EOF. So after all the entrances have passed, I must print out all the respective matrices. But I did not understand how I should make this stop criterion, IE, how to represent this EOF.

2 answers

3


Simple, use an object Bufferedreader and call the method readline() until he returns null. Remembering that the method readLine() class BufferedReader() returns a String. And as in your exercise you are dealing with a number, just convert to String for int. Example:

import java.io.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String linha;

        while ((linha = br.readLine()) != null) {
            int n = Integer.parseInt(linha);
            // TODO: lógica
        }
    }

}

Working with some sample values on Ideone.

I had already solved this exercise in the URI a few years ago (I am the 7th placed there), and I did this using exactly the same reading code I posted above (except the logic of the problem itself, obviously).

  • It didn’t work. I report a line with entries: 10 5 9 8 25, give enter and it does nothing. If then I inform another input (after enter) it prints the result. I’m a little confused about the entry condition of the problem, I don’t know if I should represent the EOF as -1 or something else. My guess is that I must provide a line with multiple entries, ie 10 20 30 5 9 45 7... and then print the respective matrices.

  • @Filipimaciel saw you updated your question after I have given a concrete example here, so I had not contributed. I adjusted a little more my answer. I hope this time it works :).

  • I must have forgotten to come back here yesterday because I was involved with the problem. Caraca Tayllan, how nice! your solution should have a very good logic. I solved the problem based on your answer there, it got a little different, but basically it’s the same thing. The part of EOF ta solved, now is the run time hehe but this I solve there in URI, thank you very much!

  • How could it be solved in Python? I cannot find anything about it anywhere, and for knowledge, it is a matter of the URI as well: https://www.urionlinejudge.com.br/judge/pt/problems/view/1215

1

You can also do it this way

 BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
 while(br.ready()){...

so when you finish the file will exit in the while loop

Browser other questions tagged

You are not signed in. Login or sign up in order to post.