Nodedesign
Nodedesign

Reputation: 445

How to read from stdin until EOF

I need a simple solution for reading this input

==*====*====
===@===
**@@@==*@@=*@*=
*=*=*=*=*=*=
==*@===*=*@=

I tried with this but it doesn't end whenever the input is read, however the string value is stored. I would need to add code after reading so that's why while should end and I can move on with my code.

Scanner sc=new Scanner(System.in);
while(true){
    if (!sc.hasNextLine()) {
        break;
    }
    String sor=sc.nextLine();
}

I appreciate the help in advance, please make me understand why the while doesn't break.

Upvotes: 4

Views: 6916

Answers (1)

Harsh Anand
Harsh Anand

Reputation: 57

I believe you need not specify the if() here as its already getting verified in while(). If input is being read from a file it should work absolutely fine. If you are reading it from some other input stream, it should terminate the line.

 Scanner sc=new Scanner(System.in);
    while(sc.hasNextLine()){
        String sor=sc.nextLine();
    }

Upvotes: 3

Related Questions