title: Loops and user input validation in java, again
body: <p>So, I think about two days back I asked a question about input validation, and how to loop programs until the user gave a valid input..</p>
  <p>So I made a calculator and I wanted to loop every step of the program so that if the user didn't put a <code>double</code> (69, 42.0) or the appropriate operator <code>char</code> (/, *, +, -) they would be stuck on that step till they got it right or otherwise closed the app entirely.</p>
  <p>So one thing I got from the last question about this is that I could make a <code>boolean</code> value called &quot;restart&quot; or something and encapsulate my entire code except for the <code>main</code> <code>method</code> obviously and at the end I could make a question that they could answer <code>true</code> or <code>false</code> and the entire app could run again. While I admit having a &quot;start over&quot; button on my app is cool and useful for all my other a projects (probably not, I'm sure this can be done more efficiently), It still didn't satiate me and my original problem.</p>
  <p>SO...</p>
  <p>I got to trying everything I could (I know stack likes people who show that they at least tried).</p>
  <p>EXAMPLE 1</p>
  <pre><code>Scanner input = new Scanner(System.in);
  
          double valX;
          
              System.out.println(&quot;Calculator Activated.&quot;);
  
                  do
                  {
                      System.out.print(&quot;Value X: &quot;);
                      valX = input.nextDouble();
                  }while (!input.hasNextDouble());
                  {
                      System.out.println(&quot;Invalid number!&quot;);
                  }
  //Didn't work ¯\_(ツ)_/¯
  </code></pre>
  <p>EXAMPLE 2</p>
  <pre><code>Scanner input = new Scanner(System.in);
  
          double valX;
  
              System.out.println(&quot;Calculator Activated.&quot;);
  
              while (!input.hasNextDouble())
              {
                  System.out.println(&quot;Value X: &quot;);
                  valX = input.nextDouble();
              }
  //neither this one...
  </code></pre>
  <p>Note that the solution you guys give me has to apply to every step of the program.</p>
  <p>And for a bit more clarity is the entire src code without the stuff I tried and the &quot;restart&quot; loop.</p>
  <pre><code>import java.util.Scanner;
  
  public class CalTest
  {
      public static void main(String[] args)
      {
          Scanner input = new Scanner(System.in);
  
          double valX, valY, divided, multiplied, added, subtracted;  //Declared all my variables...
          char operator;
          boolean restart; //Didn't need to declare it true or false since I'm using a Scanner anyway
  
          do //Start of the entire program
          {
              System.out.println(&quot;Calculator Activated.&quot;);
  
              System.out.print(&quot;Value X: &quot;); //I need a loop here...
              valX = input.nextDouble();
              
              System.out.print(&quot;Operator: &quot;); //And here...
              operator = input.next().charAt(0);
  
              System.out.print(&quot;Value Y: &quot;); //And here too..
              valY = input.nextDouble();
  
              divided = valX / valY;
              multiplied = valX * valY;
              added = valX + valY;
              subtracted = valX - valY;
  
              if (operator == '/')
                  System.out.println(&quot;Result: &quot; + divided );
              else if (operator == '*')
                  System.out.println(&quot;Result: &quot; + multiplied); //&lt;--Not sure if I need a loop with the if's
              else if (operator == '+')
                  System.out.println(&quot;Result: &quot; + added);
              else if (operator == '-')
                  System.out.println(&quot;Result: &quot; + subtracted);
              else
                  System.out.println(&quot;Invalid operator!&quot;);
  
              System.out.print(&quot;Try again? &quot;); //I also need a loop here, I think.
              restart = input.nextBoolean();
  
          } while (restart); //End of it if you declared false.
          
          System.out.println(&quot;Calculator terminated.&quot;);
      }
  }
  
  </code></pre>
  <p>At one point I tried to use the same &quot;restart the app&quot; concept and made a boolean variable for every single step in the code and it honestly was tiresome and not worth it.</p>
  <p>Also I'm just a beginner if it's a concept of the loops that I'm missing then I'm happy to learn it from you guys.</p>
  <p>Again, gracias to anyone who answers and helps contribute to my learning.</p>