title: Getting runtime error NoSuchElementException. Are there any changes required in the code?
body: <p>If a crop is grown for once, the fertility of the soil reduces by 30. After cultivation, if the land is left free for one month, the fertility increases by a factor of 2. If the fertility becomes 0, the crop cannot be grown futher. Write a jAVA program to get the initial fertility and get the number of months the land is left free after every cultivation and find the number of times the crops are successfully grown, before the fertility becomes 0. Note 1: If the fertility becomes 0 in the middle of the growth of crop, the crop stops growing. Note 2: Stop getting the input if the fertility becomes 0.</p>
  <p><strong>Input Format:</strong> First input is an integer that corresponds to the initial fertility of the soil. Next inputs are number of months the land is left free after every cultivation.</p>
  <p><strong>Output Format:</strong> Number of times the crops are grown successfully.</p>
  <p><strong>Sample Input 0</strong></p>
  <p>35</p>
  <p>3</p>
  <p>1</p>
  <p><strong>Sample Output 0</strong></p>
  <p>2</p>
  <p><strong>My Code</strong></p>
  <pre><code>class Soil{
      int initial_fert, ncultivations;
      Soil(){
          Scanner sc = new Scanner(System.in);
          initial_fert = sc.nextInt();
      }
      int countCultivations(){
          Scanner sc = new Scanner(System.in);
          int no;
          ncultivations = 0;
          while(true){
              initial_fert = initial_fert - 30; //35-&gt;5 40-&gt;10 20-&gt;-10
              if(initial_fert &lt;= 0) break; //no no yes
              no = sc.nextInt(); //3 1
  
              for(int i=0; i&lt;no; ++i)
                  initial_fert = initial_fert * 2;
              
              ++ncultivations;
          }
          return ncultivations;
      }
  }
  
  public class Solution {
  
      public static void main(String[] args) {
          Soil s = new Soil();
          System.out.println(s.countCultivations());
      }
  }
  </code></pre>
  <p><a href="https://i.stack.imgur.com/oUeLQ.png" rel="nofollow noreferrer">Error thats occuring</a></p>