我无法弄清楚为什么会出现不匹配异常,一切正常,直到我在计算机上打开它。我必须把人口普查文件读进去,我这样做了,我找到了增长率,但唯一的问题似乎是这个不匹配异常,我不明白为什么会出现不匹配错误,请帮忙。这是文件示例:
Alabama,4447100
Alaska,626932
Arizona,5130632
Arkansas,2673400
这是程序:
public static void main(String[] args) throws IOException {
File f = new File("census2010.txt");
File ff = new File("census2000.txt");
if(!f.exists()) {
System.out.println( "f does not exist ");
}
if(!ff.exists()) {
System.out.println( "ff does not exist ");
}
Scanner infile2010 = new Scanner(f);
Scanner infile2000 = new Scanner(ff);
infile2010.useDelimiter("[\t|,|\n|\r]+");
infile2000.useDelimiter("[\t|,|\n|\r]+");
final int MAX = 60;
int[] pop10 = new int[MAX];
int[] pop00 = new int[MAX];
String[] statearray10 = new String[MAX];
String[] statearray00 = new String[MAX];
double [] growtharray = new double[MAX];
int fillsize;
fillsize = fillArrayPop (pop10, statearray10, MAX, infile2010, prw);
fillsize = fillArrayPop2 (pop00, statearray00, MAX, infile2000, prw); // the mismatch exception occurs here
growthRate(growtharray, pop10, pop00, fillsize);
sortarray(growtharray, statearray10, fillsize);
printarray (growtharray, statearray10, fillsize, prw);
}
public static int fillArrayPop (int[] num, String[] statearray10, int mAX, Scanner infile2010, PrintWriter prw) throws FileNotFoundException{
int retcnt = 0;
int pop;
String astate;
while(infile2010.hasNext()){
astate = infile2010.next();
pop = infile2010.nextInt();
statearray10[retcnt] = astate;
num[retcnt] = pop;
retcnt++;
}
return (retcnt);
}
public static int fillArrayPop2 (int[] number, String[] statearray00, int mAX, Scanner infile2000, PrintWriter prw) throws FileNotFoundException{
int retcounts = 0;
int pop;
String state;
while(infile2000.hasNext()){
state = infile2000.next();
pop = infile2000.nextInt(); // the mismatch exception occurs here
statearray00[retcounts] = state;
number[retcounts] = pop;
retcounts++;
}
return (retcounts);
}
public static void printarray (double[] growth, String[] state, int fillsize, PrintWriter prw){
DecimalFormat form = new DecimalFormat("0.000");
for (int counts = 0; counts < fillsize ; counts++){
System.out.println("For the position ["+counts+"] the growth rate for " + state[counts] + " is " +form.format(growth[counts]));
prw.println("For the position ["+counts+"] the growth rate for " + state[counts] + " is " + form.format(growth[counts]));
}
prw.close();
return;
}
public static void growthRate (double[] percent, int[] pop10, int[] pop00, int fillsize){
double growthrate = 0.0;
for(int count = 0; count < fillsize; count ++){
percent[count] = (double)(pop10[count]-pop00[count])/ pop00[count];
}
}
public static void sortarray(double[] percent, String[] statearray10, int fillsize) {
for (int fill = 0; fill < fillsize - 1; fill = fill + 1) {
for (int compare = fill + 1; compare < fillsize; compare++) {
if (percent[compare] < percent[fill]) {
double poptemp = percent[fill];
percent[fill] = percent[compare];
percent[compare] = poptemp;
String statetemp = statearray10[fill];
statearray10[fill] = statearray10[compare];
statearray10[compare] = statetemp;
}
}
}
}
为什么我会收到此错误?