0

I have following below code for calculating average values in an array:

package javaapplication2;

import java.util.Scanner;


public class JavaApplication {

public static void main(String[] args) {
    Scanner user_input = new Scanner(System.in);

    int valuen;
    System.out.println("enter the amount of numbers");
    valuen = user_input.nextInt();

    while (valuen<=1){
    System.out.println("Please enter a value greater than 1");
    valuen = user_input.nextInt();
    }
    //create an array
    double[] numbers = new double[valuen];
    int i; 
    double sum = 0;

    for (i=0; i < numbers.length ; i++){
        System.out.println("Please enter a number");
        numbers[i] = user_input.nextDouble();
        sum = sum + numbers[i];

    }
     double average = sum / numbers.length;
     System.out.println("the average is" + average);


}

After entering the numbers the code just ends can someone help me display the average?

hitz
  • 1,100
  • 8
  • 12
  • what is the problem? your average calculation seems to be correct – Jordi Castilla May 26 '15 at 20:46
  • after i execute the code it does not display the average on the screen just ends – prostigus May 26 '15 at 20:47
  • 2
    I copy/pasted your code and i run it, it shows the average... – Marcello Davi May 26 '15 at 20:53
  • Your code correctly displays the average value when I run it. How are you running your program? – azurefrog May 26 '15 at 20:54
  • 1
    Are you using any IDE and not able to find console window? The code you have given should print average. – hitz May 26 '15 at 20:55
  • yes i am hitting the enter i will try another ide – prostigus May 26 '15 at 20:58
  • Can you please provide as much detail as possible about exactly how you are running your program? I am getting the average to print out both in eclipse and from the command-line. – azurefrog May 26 '15 at 21:00
  • Are you entering a non-zero value when asked how many numbers should be entered? There is no reason why this code would just terminate without printing anything about the average. – SamTebbs33 May 26 '15 at 21:01
  • i was using a program on windows called NetBeans and after pressing enter on the last number the build just ends – prostigus May 26 '15 at 21:01
  • Perhaps it is still waiting for you to enter a number and not really terminating? Which IDE are you using? – RealSkeptic May 26 '15 at 21:01
  • It prints it out, and then your program ends, so the window closes. You'll have to add something to capture user input at the very end of the program. – Andrew May 26 '15 at 21:02
  • what should i add to capture the code? – prostigus May 26 '15 at 21:04
  • 1
    Try re-opening console to see if average was printed as mentioned here [Re-Open console in netbeans](http://stackoverflow.com/questions/15835888/how-to-recover-closed-output-window-in-netbeans). Console window may close on program termination. – hitz May 26 '15 at 21:04
  • idk it won't work i guess I will just compile on linux once i get home thanks for the help guys\ – prostigus May 26 '15 at 21:08

0 Answers0