MrMiyagi MrMiyagi - 17 days ago 5
Java Question

Arrays Java beginner

I'm having trouble storing values into my

monthlySales
array. Also, how can I retrieve the values to use in other methods such as
totalSales
? Plus, I have errors in
getSales()
method.

import java.util.Scanner;
import java.text.DecimalFormat;


public class SalesTracking
{
static DecimalFormat f = new DecimalFormat ("$#,##0.00");
static Scanner input = new Scanner (System.in); //create a scanner object that reads from the keyboard (System.in)
private static final double[] monthlySales = new double[12]; //create monthlysales array
private static final String[] monthArray = {"January" , "February" , "March" , "April", "May",
"June", "July", "August", "September", "October",
"November", "December"}; //create 12-month array
static void getSales(){
int size = monthArray.length;
for (int i = 0; i < size; i++){
System.out.print("Please Enter Sales for " + monthArray[i] + ":");
monthlySales[i] = input.nextDouble();
} //get sales method to store values into monthlysales
}


public static void main(String[] args){

getSales();
totalSales();
computeHighestMonth(monthlySales);
computeLowestMonth(monthlySales);

Answer

Alright, let's break down your multiple sub-questions.

Having trouble storing values into my monthlySales array.

First of all, avoid C syntax and write your declaration as:

private static final double[] monthlySales = new double[12];

This emphasizes that monthlySales is of the type double[]. Now, for your question. The below line is what's giving you trouble.

monthlySales = input.nextDouble();

When you declared monthlySales as final, that actually means that you can't have the reference variable point to anything else. Here, you're attempting to make the variable point to a double, which can't happen both because the variable must point to a double[] and the final declaration.

You want this instead, where changing the values in the array is perfectly fine (Java's final keyword is a little weird):

monthlySales[i] = input.nextDouble();

Plus I have errors on public static void getSales() method.

This is because you've defined the getSales method inside of main(). In Java, you can't define methods inside of other methods (you can do this with classes, however).

I believe you want to strip out the getSales definition entirely, since your main client appears to be reading from standard input. If you need to access your arrays from another class, define getter methods outside of main.

Also how can I retrieve the values to use in other methods such as totalSales?

Because you've assigned the values to the static members monthlySales and monthArray, you can already access those variables from your other static methods.

Note that since you have no non-static variables or methods, you can't create multiple instances of SalesTracking meaningfully, since all instances will share the same set of data.

Errors on void(invalid type) and getSales (Illegal modifier for parameter getSales; only final is permitted)

These stem from your getSales definition inside of main. If you define the method outside or remove it entirely, you'll be fine.

EDIT: Fix your getSales() like so:

public static void getSales() {
    try (
        Scanner input = new Scanner(System.in); // try-with-resources
    ) {
        int size = monthArray.length; 
        for (int i = 0; i < size; i++){
            System.out.print("Please Enter Sales for " + monthArray[i] + ":");
            monthlySales[i] = input.nextDouble();
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

public static void main(String[] args) {
    getSales();
}