Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 496
Nodedesign Avatar asked Oct 17 '25 22:10

Nodedesign


1 Answers

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();
    }
like image 66
Harsh Anand Avatar answered Oct 19 '25 11:10

Harsh Anand