我一直在努力让这个计划的工作。我在使程序读取我创建的文件census2000
和census2010
时遇到了一些麻烦。这些包含2000年和2010年的50个州及其人口。我相信我的其余计划是正确的。我被告知使用方法找到最小的人口,最大的人口和平均水平。下面是从2000文件两行:寻找最小和最大的人口和平均水平
阿拉巴马4447100
阿拉斯加626932
下面是我的程序:
public static void main(String[] args) throws IOException {
String state = "";
int population = 0;
int p = 0, s = 0, pop = 0, stat = 0, populate = 0, sum = 0;
File f = new File("census2000.txt");
Scanner infile = new Scanner(f);
infile.useDelimiter("[\t|,|\n|\r]+");
while (infile.hasNext()) {
checksmall(p, s);
checklargest(pop, stat);
checkAverage(populate, sum);
population = infile.nextInt();
state = infile.next("/t");
System.out.println(state + "has" + population + "people");
}
System.out.println(state + "has smallest population of" + population);
prw.close();
}
public static boolean checksmall(int p, int s) {
boolean returnValue;
if (p < s) {
returnValue = true;
} else {
returnValue = false;
}
return (returnValue);
}
public static boolean checklargest(int pop, int stat) {
boolean returnVal;
if (pop > stat) {
returnVal = true;
} else {
returnVal = false;
}
return (returnVal);
}
public static int checkAverage(int populate, int sum) {
int retVal;
retVal = populate + sum;
return (retVal);
}
}
我在做什么错?
包括您正在尝试阅读的文件,以便我们可以看到格式。 – jonny2k9 2013-04-06 00:47:10
在您读取数据之前调用check *方法,并且您的文件中的所有数据只有两个变量(所以也许您将在循环中调用check *)。假设你只是想读取文件扫描的输出,那么扫描程序接下来的*测试是否匹配文件的数据格式? – n0741337 2013-04-06 00:51:44
当我这样做时,我得到了错误输入不匹配异常 – CBH 2013-04-06 00:55:04