views:

256

answers:

3

I have a homework problem Im 75% done with. My problem is that I cannot find a way to correctly display the correct value for the lowest population value and the province that has it. (Thanks in advance.):

*

  1. Write a program to read in the province data file into an array called provinceName and an array called provincePopulations. You can assume that there are 10 provinces. Write a loop to: read the data into two arrays. Analyse the array to calculate the total population of all the provinces. Write another loop to find the province with the smallest population and print that the name of that province.

*

The data is store in the following manner (provinceData.txt):

Ontario
12891787
Quebec
7744530
Nova Scotia
935962
New Brunswick
751527
Manitoba
1196291
British Columbia
4428356
PEI
139407
Saskatchewan
1010146
Alberta
3512368
NF/LB
508270

Here is my Java code:

import java.io.*;
import java.util.Scanner;

public class Slide50 
{
    public static void main(String[] args)throws IOException
    {
     File file = new File ("C:/provinceData.txt");
     Scanner in = new Scanner(file);
     int i = 0;  

     String province[] = new String[10];
     String lowProv = null;
     int pop[] = new int[10];  
     int totalPop = 0;
     int low = pop[0];

     //while there is data in the file to be processed  
     while(in.hasNext())
     {
      province[i] = in.nextLine();
      pop[i] = in.nextInt();

      //discard the \n on the line   
      in.nextLine();

      //regular processing goes here
      i++;
     }

     System.out.printf("\n\t%-16s %20s\n", "Province", "Population");
     System.out.printf("\t%-16s %20s\n", "========", "=========="); 

     //print the province population report (which includes a total) using printf
     for (i = 0; i < pop.length; i++)
     {   
      System.out.printf("\t%-16s %,20d\n", province[i], pop[i]);  
      totalPop += pop[i];

      //find the province that has the smallest population 
      //and print out the province name and its population 
      if (pop[i] < low)
            {
             low = pop[i];              
            }          
     }

     System.out.printf("\t%-16s %20s\n", "================", "=========="); 
     System.out.printf("\t%-16s %,20d\n", "Total:", totalPop);         
     System.out.println("\n\tThe province of " +  lowProv + " with a population of " +  low);
     System.out.println("\tis the least populated of all provinces."); 
    }
}

Here is my sample run based on this code:

Province                   Population
========                   ==========
Ontario                    12,891,787
Quebec                      7,744,530
Nova Scotia                   935,962
New Brunswick                 751,527
Manitoba                    1,196,291
British Columbia            4,428,356
PEI                           139,407
Saskatchewan                1,010,146
Alberta                     3,512,368
NF/LB                         508,270
================           ==========
Total:                     33,118,644

The province of null with a population of 0
is the least populated of all provinces.
+1  A: 

You have two problems:

  1. You initialize low to zero. low is initialized right after the creation of pop, so pop[0] is still 0. I would recommend just initializing it with Integer.MAX_VALUE (or moving it elsewhere).

  2. You don't update lowProv as you are updating low value. So it is always null.

notnoop
A: 

You're setting low to be 0. Set that to Integer.INT_MAX instead.

e: notnoop covered it. But yeah, you need to update lowProv as well

Simon Righarts
A: 

The easiest way to do this is to find the minimum as you loop through the file:

while (...) {
  province[i] = ...
  pop[i] = ...
  if (pop[i] < minpop) {
    minpop = pop[i];
    minprovince = province[i];
  }
  ...
}

You've gotten most of the way there with your second loop but you're setting low to be the first entry in pop before you've put anything in the pop array so seed the value with something like Integer.MAX_VALUE.

cletus