In: Computer Science
I am getting an error at linen 57 and can't figure out how to fix it.
// Java program to read a CSV file and display the min, max, and
average of numbers in it.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
// method to determine and return the minimum
number from the array
public static int minimum(int numbers[])
{
int minIdx = 0;
for(int
i=1;i<numbers.length;i++)
{
if((minIdx == -1) || (numbers[i] < numbers[minIdx]))
minIdx = i;
}
return
numbers[minIdx];
}
// method to determine and return the maximum
number from the array
public static int maximum(int numbers[])
{
int maxIdx = 0;
for(int
i=1;i<numbers.length;i++)
{
if((maxIdx == -1) || (numbers[i] > numbers[maxIdx]))
maxIdx = i;
}
return
numbers[maxIdx];
}
// method to calculate and return the average of
all numbers from the array
public static double average(int
numbers[])
{
int total = 0;
for(int
i=0;i<numbers.length;i++)
total += numbers[i];
return(((double)total)/numbers.length);
}
public static void main(String[] args) throws
FileNotFoundException {
Scanner fileScan = new
Scanner(new File("C:\\Users\\useracct\\eclipse-workspace\\Group4
Final\\src\\numbers.csv")); // open the CSV file, provide full path
to file
int numbers[] =
null;
fileScan.useDelimiter(","); // use the delimiter , to separate the
data in the file since it is a CSV file
// read till the end of
file
while(fileScan.hasNext())
{
// check if array is not created
if(numbers == null)
numbers = new int[1]; // then create it with 1 element
else
numbers = Arrays.copyOf(numbers, numbers.length+1); // expand the
array by 1 size
numbers[numbers.length-1] = fileScan.nextInt(); // populate the
array with the number read
}
fileScan.close(); //
close the file
// if file wasn't
empty
if(numbers.length >
0)
{
// display the numbers, minimum, maximum and average of
numbers
System.out.println("Number(s) read from file : ");
for(int i=0;i<numbers.length;i++)
{
System.out.println(numbers[i]+" ");
}
System.out.println();
System.out.println("Minimum number : "+minimum(numbers));
System.out.println("Maximum number : "+maximum(numbers));
System.out.println("Average number : "+average(numbers));
}
}
}
//end of program
import java.io.File; import java.io.FileNotFoundException; import java.util.Arrays; import java.util.Scanner; public class Main { // method to determine and return the minimum number from the array public static int minimum(int numbers[]) { int minIdx = 0; for (int i = 1; i < numbers.length; i++) { if ((minIdx == -1) || (numbers[i] < numbers[minIdx])) minIdx = i; } return numbers[minIdx]; } // method to determine and return the maximum number from the array public static int maximum(int numbers[]) { int maxIdx = 0; for (int i = 1; i < numbers.length; i++) { if ((maxIdx == -1) || (numbers[i] > numbers[maxIdx])) maxIdx = i; } return numbers[maxIdx]; } // method to calculate and return the average of all numbers from the array public static double average(int numbers[]) { int total = 0; for (int i = 0; i < numbers.length; i++) total += numbers[i]; return (((double) total) / numbers.length); } public static void main(String[] args) throws FileNotFoundException { Scanner fileScan = new Scanner(new File("C:\\Users\\useracct\\eclipse-workspace\\Group4 Final\\src\\numbers.csv")); // open the CSV file, provide full path to file int numbers[] = null; fileScan.useDelimiter(","); // use the delimiter , to separate the data in the file since it is a CSV file // read till the end of file while (fileScan.hasNext()) { // check if array is not created if (numbers == null) numbers = new int[1]; // then create it with 1 element else numbers = Arrays.copyOf(numbers, numbers.length + 1); // expand the array by 1 size numbers[numbers.length - 1] = fileScan.nextInt(); // populate the array with the number read } fileScan.close(); // close the file // if file wasn't empty if (numbers != null) { // Error here. numbers != null should be used here to check if there are numbers in array // display the numbers, minimum, maximum and average of numbers System.out.println("Number(s) read from file : "); for (int i = 0; i < numbers.length; i++) { System.out.println(numbers[i] + " "); } System.out.println(); System.out.println("Minimum number : " + minimum(numbers)); System.out.println("Maximum number : " + maximum(numbers)); System.out.println("Average number : " + average(numbers)); } } }