title: Is there a way to stop scanner input loop after the condition has been met?
body: <p>I am attempting to only allow the user to pick from the given of input. If they pick something other than <strong>"f", "F", "c", or "C"</strong> for the first <code>optionGate</code>, then they will be prompted to try again and looped back to the scanner. It is the same situation for the next gate, <code>optionGate2</code>.</p>
  
  <p>The gates accomplish what I wanted them to by stopping the user from putting in a different input than the options given. My problem comes in when both booleans are turned to <code>true</code> and the gates are passed. The scanner for some reason still asks for user input in an infinite loop. I can then type anything into the scanner over and over and the program runs no further.</p>
  
  <p>Any suggestions or comments about what is going on?</p>
  
  <pre class="lang-java prettyprint-override"><code>Scanner in = new Scanner(System.in);
  boolean optionGate = false;
  boolean optionGate2 = false;
  
  String tempLabel = "";    //initialize to F
  String precipLabel = ""; //initialize to in
  
  while(optionGate != true){
      System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
      String scaleT = in.nextLine();
  
      if("f".equalsIgnoreCase(scaleT)){
          tempLabel = "F";
          optionGate = true;
      }
      else if("c".equalsIgnoreCase(scaleT)){
          tempLabel = "C";
          optionGate = true;
      }
      else{
          System.out.println("Error: not an option. Try again:");
          optionGate = false;
      }
  }
  
  while(optionGate2 != true){
      System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)");
      String scaleP = in.nextLine();
  
      if("i".equalsIgnoreCase(scaleP)){
          precipLabel = "in";
          optionGate2 = true;
      }
      else if("c".equalsIgnoreCase(scaleP)){
          precipLabel = "cm";
          optionGate2 = true;
      }
      else{
          System.out.println("Error: not an option. Try again:");
          optionGate2 = false;
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong> Adding the code that follows the above snippet. (Sorry for the obvious lack of stackoverflow know-how. This is my first post on the platform).</p>
  
  <pre><code>String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
          double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
          double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values
  
          double[] updatedTemperature;
          updatedTemperature = new double[temperature.length];
  
          double[] updatedPrecipitation;
          updatedPrecipitation = new double[precipitation.length];
  
  
          while("f".equalsIgnoreCase(tempLabel)){
              for(int i = 0 ; i &lt; temperature.length ; i++) {
              updatedTemperature[i] = temperature[i];
          }
  
               if("in".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = precipitation[i];    
              }
  
          }
          else if("cm".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = (precipitation[i] * 2.54);    
              }
  
          }
         }
  
          while("c".equalsIgnoreCase(tempLabel)){
             for(int i = 0 ; i &lt; temperature.length ; i++) {
              updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
          }
  
  
               if("in".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = precipitation[i];    
              }   
  
          }
          else if("cm".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = (precipitation[i] * 2.54);    
              }
  
          }
          }
  
  
  
  
          //Processing - calculate average temperature and total precipitation
          for( int index = 0; index &lt; updatedTemperature.length; index++)
          {
              sum += updatedTemperature[index];
          }
          avg = sum/updatedTemperature.length;
  </code></pre>
  
  <p><strong>EDIT:</strong> Here is all of the code inside of my main method:</p>
  
  <pre><code>
          //Declare and initialize variables
          Scanner in = new Scanner(System.in);
          String city = "Orlando";
          String state = "FL";
          double sum = 0;
          double avg = 0;
          boolean optionGate = false;
          boolean optionGate2 = false;
  
          String tempLabel = "";    //initialize to F
          String precipLabel = ""; //initialize to in
  
          while(optionGate != true){
          System.out.println("Choose the temperature scale (F = Fahrenheit, C = Celsius:)");
          String scaleT = in.nextLine();
  
          if("f".equalsIgnoreCase(scaleT)){
              tempLabel = "F";
              optionGate = true;
          }
          else if("c".equalsIgnoreCase(scaleT)){
              tempLabel = "C";
              optionGate = true;
          }
          else{
              System.out.println("Error: not an option. Try again:");
              optionGate = false;
          }
          }
  
          while(optionGate2 != true){
          System.out.println("Choose the precipitation scale (i = inches, c = centimeters:)"); 
          String scaleP = in.nextLine();
  
          if("i".equalsIgnoreCase(scaleP)){
              precipLabel = "in";
              optionGate2 = true;
          }
          else if("c".equalsIgnoreCase(scaleP)){
              precipLabel = "cm";
              optionGate2 = true;
              in.close();
          }
          else{
              System.out.println("Error: not an option. Try again:");
              optionGate2 = false;
          }
          }
  
  
  
  
          String [] month = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
          double [] temperature ={60.9, 62.6, 67.4, 71.5, 77.1, 81.2, 82.4, 82.5, 81.1, 75.3, 68.8, 63.0};     //initialize with Fahrenheit values
          double [] precipitation ={2.4, 2.4, 3.5, 2.4, 3.7, 7.4, 7.2, 6.3, 5.8, 2.7, 2.3, 2.3};      //initialize with inch values
  
          double[] updatedTemperature;
          updatedTemperature = new double[temperature.length];
  
          double[] updatedPrecipitation;
          updatedPrecipitation = new double[precipitation.length];
  
  
          while("f".equalsIgnoreCase(tempLabel)){
              for(int i = 0 ; i &lt; temperature.length ; i++) {
              updatedTemperature[i] = temperature[i];
          }
  
               if("in".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = precipitation[i];    
              }
  
          }
          else if("cm".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = (precipitation[i] * 2.54);    
              }
  
          }
         }
  
          while("c".equalsIgnoreCase(tempLabel)){
             for(int i = 0 ; i &lt; temperature.length ; i++) {
              updatedTemperature[i] = ((temperature[i] - 32) * (5.0/9));
          }
  
  
               if("in".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = precipitation[i];    
              }   
  
          }
          else if("cm".equals(precipLabel)){
              for(int i = 0 ; i &lt; precipitation.length ; i++){
              updatedPrecipitation[i] = (precipitation[i] * 2.54);    
              }
  
          }
          }
  
  
  
  
          //Processing - calculate average temperature and total precipitation
          for( int index = 0; index &lt; updatedTemperature.length; index++)
          {
              sum += updatedTemperature[index];
          }
          avg = sum/updatedTemperature.length;
  
          //Output: display table of weather data including average and total
          System.out.println();
          System.out.println("           Weather Data");
          System.out.println("      Location: " + city +", " + state);
          System.out.println("Month     Temperature (" +  tempLabel + ")     Precipitation (" + precipLabel + ")");
          System.out.println();
          System.out.println("***************************************************");
          for( int index = 0; index &lt; temperature.length; index++)
          {
              System.out.println(month[index] + "\t   " + updatedTemperature[index] + "\t\t       " + updatedPrecipitation[index]);
          }
          System.out.println("***************************************************");
          System.out.println("Average: " + avg + "    Total: " + sum);
  </code></pre>