读取文件并将数据存储在数组中,然后找到最小的十进制值

时间:2009-11-30 02:58:47

标签: java arrays file

我的问题是我无法找到正确显示最低人口值和拥有它的省份的正确值的方法。

  

编写一个程序来读取       将省数据文件转换为数组       叫省名和数组       叫省人口。您可以       假设有10个省。       写一个循环:将数据读入       两个数组。分析数组       计算总人口       所有的省份。写另一个       循环找到省份       最小的人口和打印       该省的名称。

数据以下列方式存储(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

这是我的Java代码:

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.");   
    }
}

以下是基于此代码的示例运行:

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.

3 个答案:

答案 0 :(得分:1)

你有两个问题:

  1. 您将low初始化为零。在low创建后立即初始化pop,因此pop[0]仍为0.我建议您使用Integer.MAX_VALUE初始化它(或将其移动到其他地方)。

  2. 在更新lowProv值时,您不会更新low。所以始终是null

答案 1 :(得分:0)

您将low设置为0.将其设置为Integer.INT_MAX。

e:notnoop覆盖了它。但是,你需要更新lowProv

答案 2 :(得分:0)

最简单的方法是在循环浏览文件时找到最小值:

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

你已经完成了第二次循环的大部分工作,但是你在pop 中添加任何内容之前将你设置为pop 中的第一个条目Integer.MAX_VALUE

之类的东西