0

The first for-loop you see does not execute and I'm not sure why. It is completely ignored, I tried it in a separate method and I tried it in the main method but something seems to be ignoring but I'm not sure how to get it to run, it simply goes to the next method run in the main method.

package math;
import java.util.Scanner;


public class mathAverageValue {

    static int numOfVals;
    static double total;
    static double average;
    static double[] arr = new double[numOfVals];
    static String boole;

    public static void input() {
        Scanner s = new Scanner(System.in);
        System.out.println("How many values will be averaged ? : ");
        numOfVals = s.nextInt();
        for(int i=0; i<arr.length; i++){
                System.out.print("Enter Element No."+(i+1)+": ");
                arr[i] = s.nextDouble();
        }
    }


    public static void process() {
        for (int i=0; i < arr.length; i++) {
            total = total + arr[i];
        }

        average = total / arr.length;   
    }

    public static void output() {

        System.out.println("Your average is : " + average);

        System.out.println("Would you like to average again? Y or N : ");
        Scanner i = new Scanner(System.in);
        boole = i.next();

        if ("Y".equals(boole)) {
            input();
            output();
        }
    }

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

}
3
  • 3
    Easy static int numOfVals; means it is 0; then static double[] arr = new double[numOfVals]; is static double[] arr = new double[0]; - arr.length is 0 and the loop isn't entered. Commented Mar 3, 2018 at 21:31
  • replace for(int i=0; i<arr.length; i++) with for(int i=0; i<numOfVals ; i++) Commented Mar 3, 2018 at 21:34
  • I could do that aswell, thanks for the help. Commented Mar 3, 2018 at 21:46

4 Answers 4

2

Assign some value to static int numOfVals. Java by default assign 0 to it. Hence your for loop will never run. Also modify your array declaration like below:-

 static double arr = new double[numOfVals];
Sign up to request clarification or add additional context in comments.

3 Comments

After assigning a value to numOfVals (the OP does this), then the array should be created arr = new double[numOfVals]; The problem is the OP does the array creation and then assigns a value to numOfVals in the wrong order.
@markspace Good Catch. Modified the answer. Thanks.
Thankyou, i'm new to java so this helps !
0

The problem is that you have assigned a value to numOfVals and then created the array in the wrong order.

public static void input() {
    Scanner s = new Scanner(System.in);
    System.out.println("How many values will be averaged ? : ");
    numOfVals = s.nextInt();
    arr = new double[numOfVals];       // <-- PUT THIS HERE
    for(int i=0; i<arr.length; i++){
            System.out.print("Enter Element No."+(i+1)+": ");
            arr[i] = s.nextDouble();
    }
}

Comments

0

It is ignored because it is a zero length array:

static int numOfVals;  // This implicitly equals 0.
static double total;
static double average;
static double[] arr = new double[numOfVals]; // so this has no elements.

hence

    for(int i=0; i<arr.length; i++){   //arr.length is 0
            System.out.print("Enter Element No."+(i+1)+": ");
            arr[i] = s.nextDouble();
    }

doesn't iterate

Comments

0

According to java primitive data types initialization, all types have a default value. In your case, static int numOfVals will be assigned with 0. This is the reason why the for loop is ignored. see https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.