Java - Uninitialized "x" variable

Asked

Viewed 29 times

0

I’m having a question:

The code is as follows::

package yan.exercises.java.struct.repeat;

import java.util.Scanner;

public class exe2 {

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

        double popA;
        double popB;
        double taxA;
        double taxB;

        boolean validate = false;

        while (validate = true) {
            System.out.print("Enter the population of city A: ");
            popA = scan.nextDouble();

            if (popA > 0) {
                break;
            } else {
                System.out.println("Population city A need to be bigger than 0.");
            }

        }
        while (validate = true) {
            System.out.print("Enter the population of city B: ");
            popB = scan.nextDouble();

            if (popB > 0) {
                break;
            } else {
                System.out.println("Population city B need to be bigger than 0.");
            }
        }

        while (validate = true) {
            System.out.print("Enter the growth rate of A: ");
            taxA = scan.nextDouble();

            if (taxA > 0) {
                break;
            } else {
                System.out.println("Growth rate city A need to be bigger than 0.");
            }

        }
        while (validate = true) {
            System.out.print("Enter the growth rate of B: ");
            taxB = scan.nextDouble();

            if (taxB > 0) {
                break;
            } else {
                System.out.println("Growth rate city B need to be bigger than 0.");
            }

        }

        int count = 0;
        while (popA < popB) {
            popA += (popA / 100) * taxA;
            popB += (popB / 100) * taxB;
            count++;
        }
        System.out.println("Population A: " + popA);
        System.out.println("Population B: " + popB);
        System.out.println("Years:     " + count);
    }

}

You are returning to me the following errors:

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
    The local variable popA may not have been initialized
    The local variable popB may not have been initialized
    .....

How do I solve this problem? When I start the variable with 0, I get a return, but the program does not return the desired result.

1 answer

1


First of all, this doesn’t make sense:

boolean validate = false;
while (validate = true) {

You initialize the variable with false, then change its value to true (yes, the operator = is attributable, the while nay is comparing whether validate is equal to true). And the variable doesn’t have its value changed anywhere, so in the end it’s unnecessary: use only while (true) and ready (even because you use a break within the loop, which ensures that at some point he will be interrupted).

What happens is that for some reason the compiler can’t detect that it will enter this loop, and so he thinks that the variable will not be initialized. If you give any value to them before the while, "works", but if using while (true) also resolves.

But actually there is a lot of repetition in the code and you can simplify it. Note that the reading of the data is the same, only changes the name of the field and variable. So you could generalize to a method, sort of like this:

static double readValue(Scanner scan, String fieldName) {
    while (true) {
        System.out.printf("Enter the %s: ", fieldName);
        double value = scan.nextDouble();
        if (value > 0) {
            return value;
        } else {
            System.out.printf("The %s must be greater than 0.\n", fieldName);
        }
    }
}

There in the main you just do so:

Scanner scan = new Scanner(System.in);
double popA = readValue(scan, "population of city A");
double popB = readValue(scan, "population of city B");
double taxA = readValue(scan, "growth rate of A");
double taxB = readValue(scan, "growth rate of B");

And for the calculation, you don’t need the loop, because Bernoulli’s formula can be used (explained in detail in this answer):

double percA = 1 + (taxA / 100);
double percB = 1 + (taxB / 100);
double t = Math.ceil(Math.log(popA / popB) / Math.log(percB / percA));
System.out.println("Population A: " + popA * Math.pow(percA, t));
System.out.println("Population B: " + popB * Math.pow(percB, t));
System.out.println("Years:     " + t);

Of course, other validations can still be made. For example, if the population of A is greater than B, it can already return zero without making any calculations; if the population of A is less than B, but the growth rate of B is higher, then A will never reach B, etc.

  • Thanks!! I was really trying to remember the use while(true)!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.