Question

In: Computer Science

I am getting an error at linen 57 and can't figure out how to fix it....

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

Solutions

Expert Solution

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));
        }

    }
}

Related Solutions

In java, I keep getting the error below and I can't figure out what i'm doing...
In java, I keep getting the error below and I can't figure out what i'm doing wrong. Any help would be appreciated. 207: error: not a statement allocationMatrix[i][j];
I am supposed to map out the following and can't figure out how to do it!...
I am supposed to map out the following and can't figure out how to do it! Can somebody help? The experiment has to do with determining the simplest formula of potassium chlorate and to determine the original amount of potassium chlorate in a potassium chlorate-potassium chloride mixture by measuring the oxygen lost from decomposition. The chemical reaction is 2KClO3(s) ------> 2KCL(s) + 3 O2(g) I am supposed to map out 1. Mass of oxygen lost in the first part 2....
I'm getting an error message with this code and I don't know how to fix it...
I'm getting an error message with this code and I don't know how to fix it The ones highlighted give me error message both having to deal Scanner input string being converted to an int. I tried changing the input variable to inputText because the user will input a number and not a character or any words. So what can I do to deal with this import java.util.Scanner; public class Project4 { /** * @param args the command line arguments...
I keep getting an error that I cannot figure out with the below VS2019 windows forms...
I keep getting an error that I cannot figure out with the below VS2019 windows forms .net framework windows forms error CS0029 C# Cannot implicitly convert type 'bool' to 'string' It appears to be this that is causing the issue string id; while (id = sr.ReadLine() != null) using System; using System.Collections.Generic; using System.IO; using System.Windows.Forms; namespace Dropbox13 { public partial class SearchForm : Form { private List allStudent = new List(); public SearchForm() { InitializeComponent(); } private void SearchForm_Load(object...
Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds...
Why am I getting this error: Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0 at HW3.main(HW3.java:6) The code: import java.io.FileWriter; import java.io.IOException; public class HW3 { public static void main(String[] args) throws IOException { // 0th argument contains the name of algorithm String algo = args[0]; // 1st argument contains the name of file // Make a new file FileWriter fw = new FileWriter(args[1]); if (algo.equals("p1")) { // 2nd argument comes in the form of...
I can't figure out how to create the decision tree with the sending a messaged situation!...
I can't figure out how to create the decision tree with the sending a messaged situation! The crew of Endurance can visit two planets (Mann’s and Edmunds’). They can choose to visit neither planets, one of the two planets, or both planets. The characteristics of Mann’s planet are below: • 30% chance of finding a perfectly habitable planet • can support all of Earth’s current population if it is • can support none of Earth’s population if it is not...
This is in Python I am getting an error when I run this program, also I...
This is in Python I am getting an error when I run this program, also I cannot get any output. Please help! #Input Section def main(): name=input("Please enter the customer's name:") age=int(input("Enter age of the customer: ")) number_of_traffic_violations=int(input("Enter the number of traffic violations: ")) if age <=15 and age >= 105: print('Invalid Entry') if number_of_traffic_violations <0: print('Invalid Entry') #Poccessing Section def Insurance(): if age < 25 and number_of_tickets >= 4 and riskCode == 1: insurancePrice = 480 elif age >=...
Syntax error in C. I am not familiar with C at all and I keep getting...
Syntax error in C. I am not familiar with C at all and I keep getting this one error "c error expected identifier or '(' before } token" Please show me where I made the error. The error is said to be on the very last line, so the very last bracket #include #include #include #include   int main(int argc, char*_argv[]) {     int input;     if (argc < 2)     {         input = promptUserInput();     }     else     {         input = (int)strtol(_argv[1],NULL, 10);     }     printResult(input);...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how...
I can't seem to figure out what the hybridization of PF6(-) is. could someone explain how to find it correctly for me?
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter...
Can't figure out what I am doing wrong. Please let me know. Sample output: Please enter the name of the weather data file: temperatures.txt Tell me about your crew’s preferred temperature for sailing: What is the coldest temperature they can sail in? 60 What is the hottest temperature they can sail in? 80 Month 1: 93.3 percent of days in range. Month 2: 20.0 percent of days in range. Month 3: 58.1 percent of days in range. Month 4: 100.0...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT