Amen
Amen

Reputation: 1633

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.

Upvotes: 0

Views: 669

Answers (1)

Aniruddha Sarkar
Aniruddha Sarkar

Reputation: 485

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.

Upvotes: 6

Related Questions