it gives me a arithmeticException on this line System.out.println("\nTotal = " + dSum + " - Average " + (dSum/divider) );

import java.util.*;
public class Lab1A{
    public static void main(String [] args){

        Scanner input = new Scanner (System.in);  
        int [] k = new int [20];
        int [] d = new int [20]; 
        String option= " "; 

    for (int i=0; i<25; i++ ){

        double random = Math.random();

        int x = (int) (random*100);

        int y = (int)x + 1; //Add 1 to change the range to 1 - 100 instead of 0 - 99

        System.out.print("Random Number: ");
        System.out.print(y);

        System.out.println("\nEnter Option: (K, D, Q): ");
        option =input.next();

        if ( option.equalsIgnoreCase("K") ){
            k[i] = x;
        }
        else if (option.equalsIgnoreCase("D")){
            d[i] = x;
        }
        else if (option.equalsIgnoreCase("Q")){
            break; 
        }
       } int dSum = 0;
         int kSum = 0; 
         int dAvg = 0; 
         int kAvg = 0;
         int divider = 0;
    System.out.print(" You discarded: \n"  );
    if(d[0] != 0){
        for (int discard = 0; discard< d.length -1; discard++){
            if (d[discard] != 0){
                System.out.print(d[discard] + " ");
                dSum += d[discard];
                divider ++; 
                }
            else 
                break;
        }
    } 
    System.out.println("\nTotal = " + dSum + " -  Average " + (dSum/divider) );
    divider = 0; 
        System.out.print(" You kept: \n"  );
        if(k[0] != 0){
            for (int keep = 0; keep< k.length -1; keep++){
                if (k[keep] != 0){
                    System.out.print(k[keep] + " ");
                    kSum += k[keep];
                    divider++; 
                    }
                else 
                    break;






       } 
      }
        System.out.println("\nTotal = " + kSum + " -  Average " + (kSum/divider) );
    }
}

Recommended Answers

All 3 Replies

So line 70. What if divider is zero? Did you print each variable to see if that happened?

Reading my mind I see Mr. Proffitt... :-)

I figured it out thank you for the input ..

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.