0
import java.util.Scanner; 
public class Main
{
  public static void main(String[] args)
  {
    Scanner keyboard = new Scanner(System.in);
    double score = 0, avg, sum = 0;
    int index = 0, count = 0, num = 0;

    readInput(keyboard, num);
    double Test[] = new double[num];
    System.out.print("\n");

    while(count < Test.length)
    {
      System.out.printf("Enter your test score #%d:", count + 1);
      try
      {
        score = keyboard.nextDouble();
      }

      catch(java.util.InputMismatchException e)
      {
        System.out.print("You have entered a non-numerical value, please try again\n");
        keyboard.next();
        continue;
      }
      if (score >= 0)
      {
        Test[count] = score;
        sum = sum + score;
      }
      else
      {
        System.out.print("You have entered an invalid grade, please try again\n");
        continue;
      }
      count++;
    }

    double maxValue = Test[0];
    for (int i = 1; i < Test.length; i++) 
    {
      if (Test[i] > maxValue) 
      {
        maxValue = Test[i];
      }
    }  

    double minValue = Test[0];
    for (int i = 1; i < Test.length; i++) 
    {
      if (Test[i] < minValue) 
      {
        minValue = Test[i];
        index = i;
      }
    }

    System.out.print("\nThe highest value is: " + maxValue + "\n");
    System.out.print("The lowest value is: " + minValue + "\n");

    avg = sum / Test.length;
    System.out.printf("Your grade average is: %.1f \n\n", avg);

    System.out.print("Would you like to drop the lowest grade: Y or N:");
    char choice = keyboard.next().charAt(0);

    if (choice == 'Y' || choice == 'y')
    {
      double newSum = sum - minValue;
      double newAvg = newSum / (Test.length - 1);

      System.out.print("\nYour old scores were: ");
      System.out.print(Test[0]);
      for (int i = 1; i < Test.length; i++) 
      { 
        System.out.print(", " + Test[i]);
      }

      System.out.print("\n\nYour new scores are: ");
      for (int i = 0; i < Test.length; i++) 
      { 
        if (index != 0 && i != index)
        {
          if (i >= 1)
          {
            System.out.print(", "); 
          }  
          System.out.print(Test[i]);
        } 
        else if (i != index)
        {
          if (i >= 2)
          {
            System.out.print(", "); 
          }  
          System.out.print(Test[i]); 
        }
      }

      System.out.printf("\n\nYour new average is: %.1f\n", newAvg);
    }
    else if (choice == 'N' || choice == 'n')
    {
      System.out.print("Your average has stayed the same.\n");
      return;
    }
    else
    {
      System.out.print("You have entered an invalid response. Bye.\n");
      return;
    }

  }

  public static int readInput(Scanner keyboard, int num)
  {
    System.out.print("How many scores would you like to enter:");
    try
    {
      num = keyboard.nextInt();
    }

    catch(java.util.InputMismatchException e)
    {
      System.out.print("You have entered a non-numerical value.\n");
      readInput(keyboard, num);
    }

    if (num < 0)
    {
      System.out.print("You have entered a negative value.\n");
      readInput(keyboard, num);
    }
    else if (num == 0)
    {
      System.out.print("If you have no test grades then you do not need this program.\n");
      readInput(keyboard, num);
    }
    return num;
  }
}

The program keeps failing and telling me I'm returning the value wrong. num needs to put into the array around line 10 but I'm having trouble getting the value to return. I need to protect against user input error which is why I'm using the Input Mismatch Exception but in order to return them to the beginning if they mess up I was told I needed to use a separate function. However, this sometimes results in an infinite loop of the function. If anyone can help with a new way of doing this or how to fix what I am currently doing that would be a huge help.

Kill_me
  • 1
  • 1
  • What's the point of returning a number/value when there's nothing to receive it? – Tom Oct 24 '17 at 22:10
  • What do you mean? I have readInput(keyboard, num); double Test[] = new double[num]; – Kill_me Oct 24 '17 at 22:14
  • 1
    Java is pass by value, your approach assumes it is pass by reference. Also, when you recurse - you need to return the value you recurse. Also, on the `InputMismatchException` you need to consume the non-int token, or it's still pending and you enter an infinite loop. – Elliott Frisch Oct 24 '17 at 22:22
  • Ok I know how to fix the token thing but I'm not understanding the pass by reference stuff – Kill_me Oct 24 '17 at 22:28
  • Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Main.main(Main.java:52) exited with non-zero status Im getting this error – Kill_me Oct 24 '17 at 22:38
  • Here you can receive a detailed explanation of what is pass by reference and pass by value: https://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value. Regarding your last comment, you will be probably trying to access the array when it is empty. That's why you receive ArrayIndexOutOfBoundsException: 0. Make sure that by that line, your array is filled with some data. – Marcelo Tataje Oct 24 '17 at 22:42

0 Answers0