I am trying to print the largest value from an array set but I keep getting an out of bounds error. I am unsure of how exactly fix it. Here is my code:
Scanner console = new Scanner(System.in); System.out.print("Please enter the name of the input file: "); String inputFileName = console.nextLine(); Scanner in = null; try { in = new Scanner(new File(inputFileName)); } catch (FileNotFoundException e) { System.out.print("Error!"); e.printStackTrace(); } int n = in.nextInt(); double[] array = new double[n]; for (int i = 0; i < array.length; i++) { array[i] = in.nextDouble(); } console.close(); double largest = array[n]; // Exception occurs here for (int i = 0; i < n; i++) { if (array[i] > largest) { largest = array[i]; } } System.out.println("The largest value in the data is: " + largest); Any help is greatly appreciated.