Search code examples
javawhile-loopio

FileInputStream read multiple lines inside while loop due to if condition


Actually this is my code :

inputStream = new FileInputStream(file);
sc = new Scanner(inputStream, "UTF-8");
while (sc.hasNextLine()) {
    String line = sc.nextLine();
    if(line.contains("blablbal") {
         // **take the next five line and put them inside string or something** 
    }
}

Is it possible to take the next five lines and put them inside string object?

And if there is another occurrence of the condition within these 5 lines how to treat that case?

Any idea?


Solution

  • Try with below code:-

              String[] lines = new String[20];
              //if you want to store more lines increase the size of the array
              int j = 0;
              //manage below while loop condition j < 20 based on array size and change the 
              //number from 20 to that number. For example if array size 50 make j < 50
              while (sc.hasNextLine() && j < 20) {
                        String line = sc.nextLine();
                    if(line.contains("blablbal") {
                       int i = 0;
                       while(sc.hasNextLine() && i < 5){
                         line = sc.nextLine();
                         if(line.contains("blablbal") {
                            continue;
                         }
                         lines[j++] = line;
                         i++;
                         if ( j >= 20 )//change this according to the size of array as well
                            break;
                       }
                     }