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;
num = 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 val)
{
System.out.print("\nHow many scores would you like to enter:");
val = 0;
try
{
val = keyboard.nextInt();
}
catch(java.util.InputMismatchException e)
{
System.out.print("You have entered a non-numerical value.\n");
keyboard.next();
readInput(keyboard, val);
}
if (val < 0)
{
System.out.print("You have entered a negative value.\n");
readInput(keyboard, val);
}
else if (val == 0)
{
System.out.print("If you have no test grades then you do not need this program.\n");
readInput(keyboard, val);
}
return val;
}
}
The error is due to the function at the bottom of the code.
This is my output currently:
How many scores would you like to enter: h You have entered a non-numerical value.
How many scores would you like to enter: 8 If you have no test grades then you do not need this program.
How many scores would you like to enter: 8
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Main.main(Main.java:53) exited with non-zero status
It seems to not recognize the second value entered. The program thinks its zero even though I entered 8 and I cant figure out why. And on the third entry after an invalid entry the program crashes. Note: if the user inputs a valid value on the first try the program works fine. It's only when the user inputs invalid input first that this error happens.
EDIT: I understand that the error is because my array has no values in it but I am asking why this is happening. The value is not returning from the function after invalid input is entered so the array is trying to run while empty.