0

I am trying to create and array that can store floating point numbers anywhere from 0.00 to 100.00. I am having a problem with the hArray, going back and fourth from it saying it needs to be initialized and trying numerous ways to do that, it tells me to change it to doublehArray[] = null;. but when i try that no valuse are stored in it.

the problem occurs between line 3 and also line 27 which is hArray[i}] = rnum

Ignore the print line command i was just using those to check errors.

public static void main(String[] args) {
    int i;
    double hArray[];
    int nYears = 0, y = 0;
    double rMax = 0.00,rMin = 100.00;

    get input check if between 1-80
    while(y == 0){
    String userData = JOptionPane.showInputDialog
                               ("Enter number of years");
    nYears = Integer.parseInt(userData);

    if (nYears > 1 && nYears <= 80 )
        y = 1;

    }

     reset y to = 0
    y = 0;
    System.out.println("step1 "+ nYears+ " "+ y);
    while(y <= nYears){
        System.out.println("step2");
        for(i = 0; i < 12; i++){
            System.out.println("step3");
            Random rand = new Random();
            double rNum = rand.nextFloat() * (rMax - rMin) + rMin;
            hArray[i] = rNum;
            System.out.println("step4" + hArray[i]);
        }
2
  • Your code contains an infinite loop. Make sure y is incremented in the second while loop. Commented Mar 23, 2016 at 22:56
  • Sorry it was there I deleted a bit to much to shorten it to post here but thanks for point it out. Commented Mar 23, 2016 at 23:18

2 Answers 2

2

It is important to note some things in Java. First, this line is 'declaring a reference':

double hArray[];

All this does is say that there will be an array of type double and gives that array the name hArray. That array is not actually initialized. Then, at this line:

hArray[i] = rNum;

... rNum can't actually be put anywhere because the array doesn't exist.

You could do something like this at first to 'initialized' the array to length 12:

double[] hArray = new double[12];

Then the second line will work. Note that if you try to reference a location greater than 12 this will cause an error: the size of the list here is considered immutable.

Alternatively, you can get away from using primitives and use a Collection. In this case a List:

List<Double> hArray = new ArrayList<>();

And then, later:

hArray.add(rNum);

This prevents you from needing to know how big the array will need to be from the beginning. The List will autoscale. Also, it will not assume a default value: by initializing an array of primitives a default value equivalent to 0 will be placed in the array. Avoiding the use of primitives is considered to be a best practice.

Sign up to request clarification or add additional context in comments.

4 Comments

Make sure the ArrayList uses generics instead of being a raw type. Change it to new ArrayList<Double>() or even new ArrayList<>()
@4castle List<Double> hArray = new ArrayList(); already uses generics. The type of the new list is inferred by Java. Further, it's better to use List than ArrayList as the type of the variable unless there is a specific reason to know the implementing class.
I'm not debating your use of List. I'm just saying that according to this the lack of generics in the constructor will generate an "unchecked conversion warning" despite the fact that it won't break anything.
Fair enough. Threw the empty generics indicator in there.
1

double hArray[] declares an array, but doesn't allocate it.
Also remember that arrays are fixed-size.

Since you code needs an array of 12 values, you should change to:

double[] hArray = new double[12];

The array will now be allocated with 12 values, all defaulting to 0.0.

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.