Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Issue with storing to Array
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
chromium




PostPosted: Thu Apr 03, 2014 7:30 pm   Post subject: Issue with storing to Array

Im trying to populate an array using a for loop. Here is my code:

code:

import java.awt.*;
import hsa.Console;
import java.io.*;
import java.util.*;
import java.text.*;

public class P1_3_7
{
    static Console c;   
    static double[] pop97;
    static double[] pop01;

    static double[] expPopArray;

    public static void main (String[] args)
    {
        c = new Console (28, 80);

        String cities[] = {"Toronto", "Montr?al", "Ottawa-Hull", "Calgary", "Edmonton", "Calgary", "Qu?bec", "Winnipeg", "Hamilton", "London", "Kitchener",
            "St. Catharines-Niagara", "Halifax", "Victoria", "Windsor", "Oshawa", "Saskatoon", "Regina", "St. John's", "Chicoutimi-Jonqui?re",
            "Sudbury", "Sherbrooke", "Trois-Rivi?res", "Thunder Bay", "Saint John"};

        c.print ("Enter a year greater than 2000: ");
        int year = c.readInt ();
        int yearDiff = year - 1997;

        c.clear ();
        c.println ("Top ten expected populations for the year " + year + " are (in thousands): \n");

        for (int i = 0 ; i < 25 ; i++)
        {
            try
            {
                BufferedReader br = new BufferedReader (new FileReader (new File ("popCities97to01.txt")));
                pop97 = new double [25];
                pop01 = new double [25];

                String line = "";
                for (int j = 0 ; j <= 24 ; j++)
                {
                    line = br.readLine ();
                    pop97 [j] = Double.parseDouble (line.substring (0, 9));
                    pop01 [j] = Double.parseDouble (line.substring (12, 21));
                }
                br.close ();
            }
            catch (IOException e)
            {
                c.print ("File not found.");
            }

            double diff = pop01 [i] - pop97 [i];
            double pop = pop97 [i];
            double factor = Math.pow (1.0 + diff / pop, 1 / 4.0);
            double expPop = (pop * (Math.pow (factor, yearDiff)));

            expPopArray = new double [25];
            expPopArray [i] = expPop;
           
           // c.println(expPopArray[i]); // If I try to print the array from here it works

        }

        c.print (expPopArray [2]);  // If I try to print the array from here it just returns 0

    }
}


My problem is with the expPopArray. When I leave the print line inside of the for loop (the line i commented out), it returns the correct values. However if I try to print the array from outside of the loop it always returns 0. I need to be able to access the array from outside of the loop. Can someone tell me where im going wrong? Thanks.
Sponsor
Sponsor
Sponsor
sponsor
Dreadnought




PostPosted: Thu Apr 03, 2014 8:10 pm   Post subject: Re: Issue with storing to Array

I'm not a java guy but this caught my eye

code:
expPopArray = new double [25];   <----  Probable culprit!
expPopArray [i] = expPop;


I'm pretty sure this creates a new array of 25 doubles. So every time you go through the loop you make a new array (throwing away the old one) and store expPop at the ith position.

You probably want to create your array outside the loop and then populate it with values.
chromium




PostPosted: Fri Apr 04, 2014 6:16 am   Post subject: Re: Issue with storing to Array

Dreadnought @ Thu Apr 03, 2014 8:10 pm wrote:
I'm not a java guy but this caught my eye

code:
expPopArray = new double [25];   <----  Probable culprit!
expPopArray [i] = expPop;


I'm pretty sure this creates a new array of 25 doubles. So every time you go through the loop you make a new array (throwing away the old one) and store expPop at the ith position.

You probably want to create your array outside the loop and then populate it with values.


Thanks, that was the problem.
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 3 Posts ]
Jump to:   


Style:  
Search: