I need help to create a program that will accumulated sales for any given month.
The total for each month will be stored in an array. There can be more than one entry for any given month and they do not have to be entered in any order.
After all data has been entered, display the name of the month and its total sales. Accumulate each of the monthly totals into a yearly total and display the yearly total sales and the average monthly sales for the year.
the user will enter the month and sales.
That what I have so far.
I need it a soon as possible.
thank youInline Code Example Here

public static void main(String[] args) {
        // TODO code application logic herepublic static double computeAverageSales(double monthlySales[]) { 
    Scanner keyboard = new Scanner(System.in);  

        String [] monthArray = {
            "January", "February", "March", "April", "May",
            "June", "July", "August", "September", "October",
            "November", "December"};
        int month;
                double averageSale;
        double sum = 0.0;
            char question;
                double[]sales=new double[12];         

         do{   

            int i=1;    
            System.out.println("Enter  Month:"   );
            month = keyboard.nextInt();
            System.out.println("Enter Sales for this Month:"   );
            sales[i]=keyboard.nextDouble();  
                question =0;
                   System.out.println("Enter more sales Y/N:"  );  
                   question=keyboard.next().charAt(0);  

                      sum +=sales[i];

               }
    while ( question == 'Y');

       System.out.println("totale sales :"+ sum  );
        System.out.println("Average sales :"+ sum/12  );    

     }
  }

Dani AI

Generated

is spot on about writing the steps first. For this assignment the model is simple: keep a running total for each month (12 buckets), accept any number of entries in any order, then print each month’s total, the yearly total, and the average (yearly total divided by 12). Also, as noticed, be careful to update the array element that corresponds to the month the user entered; do not use a fixed index.

Here is a compact example that accumulates multiple sales per month and prints nicely formatted results. It uses java.time.Month to get month names, which avoids maintaining a separate name array.

import java.time.Month;
import java.time.format.TextStyle;
import java.util.Locale;
import java.util.Scanner;

public class SalesByMonth {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    double[] monthly = new double[12];

    while (true) {
      System.out.print("Enter month (1-12) or 0 to finish: ");
      int m = sc.nextInt();
      if (m == 0) break;
      if (m < 1 || m > 12) { System.out.println("Invalid month."); continue; }

      System.out.print("Enter sale amount: ");
      monthly[m - 1] += sc.nextDouble();
    }

    double yearTotal = 0;
    for (double v : monthly) yearTotal += v;

    for (int i = 0; i < 12; i++) {
      String name = Month.of(i + 1).getDisplayName(TextStyle.FULL, Locale.ENGLISH);
      System.out.printf("%-10s %10.2f%n", name, monthly[i]);
    }
    System.out.printf("Year total: %,.2f%n", yearTotal);
    System.out.printf("Monthly average: %,.2f%n", yearTotal / 12.0);
  }
}

Tips:

  • Input validation matters; reject months outside 1..12 and keep looping.
  • If your instructions require Y/N prompts, read a char, convert with Character.toUpperCase, and compare to 'Y'.
  • For real money, prefer BigDecimal (or store cents as long) to avoid floating-point rounding surprises.

Recommended Answers

All 5 Replies

Write the solution in pseudo code first - the exact steps and processes you need to follow to get the correct answer, and then write the code to reflect that. DO NOT start with code! People keep asking me why I spend so much time on analysis and design (and stuff like this). My answer is simple - I take 80% of the time to model the problem correctly, then I can write the code in a day or two! And it will work the first time... After 35 years as a professional software engineer, I can say without exception that this is the best approach. Other engineers will write the code in a couple of days, and then spend twice as much time on re-design and bug fixing than I did on the design/analysis phase in the first place!

Thank you for all the informations.
This is my first java class, I try to learn as much as I can,I spent more than to weeks to create this program but I coudn't that why I asked for help.

rubber man is right. Start by getting the logic right in your head or on a sheet of paper. Doesn't matter how you write that down. When you have that clear in your own mind, then start coding.

You didn't say what is your exact problem, or what help you need exactly. But I did notice that line 19 you read the month number into a varsiable called month, but on line 21 you use i instead of month.

Import Java . Util
Public class ABC
//start of class
Public static void main(string args);
//start of main 
Scanner sc =new scanner(system.in)
Int=0
Int=0
System.out.println(enter your sales(in rupees)
Sales=sc.next int ();
Else if sales :>1000 && sales <(5000)
Com=sales(10/100);
Else if sale >5000 && sales >= (10000);
Com=sales

You haven’t asked any questions, but I guess you want to know what’s wrong with that code…
Using upper case where lower case is needed, and vice-versa
Missing open and close parentheses
Missing semicolons
Missing quote marks on strings
(Etc)
Programming languages are totally rigid about stuff like that. There’s no flexibility.

Ps. Next time start your own thread, don’t hijack an old one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.