0

In C i do this:

int main(){
int N = 0;
while(scanf("%d",&N) != EOF){ // <--- how to do this condition in java?
... Code ...
}

My input is some numbers: 10 20 5 9 7 6... and when i hit enter my output is some matrix.

Print with example

I know there is another topic, here, similar to this, but not solve my problem.

Any suggestion?


My problem is solved by Elliot, i just do this:

import java.io.IOException;
import java.util.Scanner;

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

        int N = 0;
        @SuppressWarnings("resource")
        Scanner scanner = new Scanner(System.in);           

        while(scanner.hasNextLine()){

            N = (int) scanner.nextDouble();
            ... code ...
          }
      }

Working fine.

Community
  • 1
  • 1
  • 2
    Have you any code yet? – mrbela Apr 12 '16 at 12:35
  • Java isn't `C`, and EOF is usually represented by `-1` in Java. – Elliott Frisch Apr 12 '16 at 12:35
  • You should figure out pretty quickly that you're not using ``scanf`` in java. Start from there and see if you really need something like EOF. – f1sh Apr 12 '16 at 12:37
  • @ElliottFrisch actually EOF is normally represented by `null` – nafas Apr 12 '16 at 12:41
  • @nafas Not with [`InputStream.read()`](https://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read%28%29); of course it depends on how you read the File. – Elliott Frisch Apr 12 '16 at 12:50
  • @mrbela, donsen't matter my code, it's running, but my problem is that condition with the while, how can i represent that in java. – Filipi Maciel Apr 12 '16 at 12:51
  • @ElliottFrisch, I want to pass a line of input, like: 10 20 30 5 9 [enter]. And when i hit enter my code is do stuff. And after this enter, it's supposed to be able to do the same thing again, and again, and again. – Filipi Maciel Apr 12 '16 at 12:51
  • 1
    Use a [`Scanner`](http://docs.oracle.com/javase/8/docs/api/java/util/Scanner.html) and `while (scanner.hasNextLine())` and `scanner.nextLine()` to operate on lines. – Elliott Frisch Apr 12 '16 at 12:59
  • @ElliottFrisch, you solve my problem, put an answer so a can mark as solved. – Filipi Maciel Apr 12 '16 at 13:16

0 Answers0