title: Java Creating a new method for user input error with do-while and for-loops
body: <p>Alright, so I have this beauty of a code that I want to squish into a new method. So far I figured obviously the error reports will be inside the method. However, it's not as simple as that because while the grade count is an int the grade itself is a double and calculated under a for loop. Many ways I've tried do not even accept an input from the user.</p>
  
  <p>I've tried looking around for a better example than a simple method format, but I have yet to find something similar to my problem. Is there a way to put both grade count and actual grade within the method for it to work? I've tried condensing code within a method before, but it was with a much simpler code than this one.</p>
  
  <p>I get the idea of methods, but actually doing them is confusing. Help is super appreciated!!!!</p>
  
  <pre><code>public static void main(String[] args) {
      // create a console object from the Scanner class
      Scanner input = new Scanner(System.in);
  
      System.out.println("Welcome to average grade calculation service!");
  
      String userChoice = null;
  
      // Use a loop to repeat the average grade calculation
      do {
          // Use a loop to read the number and check the error
          boolean isCorrectNumber = false;
  
          // First, get the number of grades
          int numberOfGrades = 0;
  
          // Loop until the user enters a numeric number of grades
          do {
              // Prompt the user to enter the number of grades
              System.out.print("\nEnter the number of grades: ");
  
              // Check if the number of grades is not numeric 
              if (!input.hasNextInt()) {
                  // Display number input error
                  System.out.println("Error: The number of grades must be a number.");
                  input.nextLine(); // flush out the input
              } else {
                  // Read the number of grades
                  numberOfGrades = input.nextInt();
  
                  // Check if the number of grade is negative
                  if (numberOfGrades &lt; 0) {
                      // Display the non positive number error
                      System.out.println("Error: The number of grades must be positive.");
                  } else
                      isCorrectNumber = true; // End the data validation loop
              } 
  
          } while (!isCorrectNumber); // continue until getting a correct data
  
          //*** Second, get all these grades to compute the sum of the grades
  
          // Declare variables for the sum of the grade and grade
          double sumOfGrades = 0;
          double aGrade = 0;
  
          // Use a loop to read each grade
          for (int i = 1; i &lt;= numberOfGrades; i++) {
  
              // Use a loop to read and validate each grade
              isCorrectNumber = false;
              do {
                  // Prompt the user to enter a grade
                  System.out.print("Enter grade " + i + ": ");
  
                  // Check if the grade is not numeric
                  if (!input.hasNextDouble()) {
                      // Display grade input error
                      System.out.println("Error: The grade must be a number.");
                      input.nextLine(); // flush out the input
                  } else {
                      // Read the grade
                      aGrade = input.nextDouble();
  
                      // Check if the grade is negative
                      if (aGrade &lt; 0)
                          // Display negative grade input error
                          System.out.println("Error: The grade must be zero or positive.");
                      else
                          isCorrectNumber = true; // End the grade validation loop
                  }
              } while (!isCorrectNumber); // continue until getting a correct grade
  
              // Add the grade to the sum and continue the loop
              sumOfGrades += aGrade;
          }
          input.nextLine();
  
          // Compute the average grade only if the sum is greater than 0
          double averageGrade = (sumOfGrades &gt; 0)? sumOfGrades / numberOfGrades : 0;
  
          // Display the average grade
          System.out.printf("\nThe average grade is %4.2f\n",  averageGrade);
  
          // Prompt if the user want to continue the calculation
          System.out.print("\nDo you want to continue (yes/no)? ");
          userChoice = input.nextLine();
          System.out.println("");
  
          // Continue the loop if the user wants to continue the calculation
      } while (userChoice.equalsIgnoreCase("yes")); 
  
      // Display an acknowledgement message
      System.out.println("Thank you and have a nice day!");
  
      input.close();      // release the console object
  }
  </code></pre>