简体   繁体   中英

how can I implement java EOF

I'm learning java from the very beginning and I'm trying to accept a problem in a programming site, Its very basic, all i need to do is to print a+b till the end of file, I searched everywhere for EOF and all of them implemented an end of file,supposing a real file, but in the problem I'm writing the code of, I shouldn't use actual file.
I use this code in C++:

#include<iostream>;
using namespace std;
int main()
{
 int a,b;
 while(cin>>a)
  {
   cin>>b;
   cout<<a+b<<endl;
  }
}

now I kinda converted it in this way to java:

package a.b;
import java.util.*;  

public class AB {  

static Scanner in=new Scanner(System.in);  

public static void main(String[] args) {
    int a,b;
    a=in.nextInt();
    while(in.nextInt()!=null)
    {
        b=in.nextInt();
        System.out.println(a+b);
        a=in.nextInt();
    }

 }
}

but I don't know how to implement an EOF for it. any help would be appreciated.

system default input stream has no eof ... rather you can implement:

it should not be

    in.nextInt()!=null

but rather

     in.hasNextInt()

or you may get a line

      in.nextLine();

and then extract each number separately by your own method.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM