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 keep getting minor errors I can't figure out and I don't know how to convert...
I keep getting minor errors I can't figure out and I don't know how to convert decimal .10 to percentage 10% either.   With these functions defined now expand the program for a company who gives discounts on items bought in bulk. Create a main function and inside of it ask the user how many different items they are buying. For each item have the user input a price and quantity, validating them with the functions that you wrote. Use your...
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...
While getting ready for work one morning I encountered something I can't figure out. I wear...
While getting ready for work one morning I encountered something I can't figure out. I wear glasses and without them objects are blurry unless I get very close, within a few inches. I always take my glasses off when I blow dry my hair in the morning. While standing in front of the mirror in my bathroom I can see a reflection of the tv screen in the bedroom. It is blurry when my glasses are off and much clearer...
Why am I getting this error using 000webhost , but I do not get this error...
Why am I getting this error using 000webhost , but I do not get this error running XAMPP? Warning: Cannot modify header information - headers already sent by (output started at /storage/ssd2/006/14881006/public_html/test/index.php:1) in /storage/ssd2/006/14881006/public_html/test/index.php on line 8 Code is below. ******************************************************************************************************************** <!DOCTYPE html> <?php    //Determine if the submit button has been clicked to set the cookie name and value    if (isset($_POST['name'])){            $cookie_name = $_POST['name'];            $cookie_value = $_POST['value'];            setcookie($cookie_name, $cookie_value,...
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...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt...
Getting an error with my for loop.  How do I correct it?  I am supposed to: Continuously prompt for the number of minutes of each Rental until the value falls between 60 and 7,200 inclusive. For one of the Rental objects, create a loop that displays Coupon good for 10percent off next rental as many times as there are full hours in the Rental. ///////////////////////////////RentalDemo package java1; import java.util.Scanner; public class RentalDemo { public static void main(String[] args) {    Rental object1 =...
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 >=...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT