import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
public class Salaries
{
public static void main(String[] args) throws FileNotFoundException
{
String filename = args[0];
Scanner in = new Scanner(new File(filename));
int sum = 0;
in.nextLine();
while(in.hasNextLine())
{
int totalSalary = 0;
String line = in.nextLine();
String[] lineArray = in.nextLine().split("\\s+");
for(int i = 0; i <= lineArray[i].length(); i++)
{
String arrayInts = lineArray[i];
if(lineArray[i].contains("$"))
{
arrayInts = arrayInts.replace("$","");
arrayInts = arrayInts.replace(",","");
totalSalary = Integer.parseInt(arrayInts);
sum = sum + totalSalary;
}
}
}
System.out.printf("$%,d\n",sum);
sum = 0;
}
}
我想不通爲什麼我總是得到一個越界的錯誤。我的代碼應該通過文本文件並收集數字並創建總計。似乎無法找到出了什麼問題
文本文件下面:
Campus President Salary Housing Auto Allowance
Bakersfield Horace Mitchell $285,000 $50,000 $12,000
Channel Island Richard R. Rush $275,000 $60,000 $12,000
Chico Paul J. Zingg $279,500 $50,000 $12,000
Dominguez Hills Mildred Garcia $295,000 provided $12,000
East Bay Mohammad H. Qayoumi $276,055 $60,000 $12,000
Fresno John Welty $299,000 provided $12,000
Fullerton Milton A. Gordon $295,000 provided $12,000
Humboldt Rollin C. Richmond $297,870 $50,000 $12,000
Long Beach F. King Alexander $320,329 provided $12,000
Los Angeles James Rosser $325,000 $60,000 $12,000
Maritime William B. Eisenhardt $258,680 provided $12,000
Monterey Bay Diane F. Harrison $270,315 provided $12,000
Northridge Jolene Koester $295,000 provided $12,000
Pomona J. Michael Ortiz $292,000 provided $12,000
Sacramento Alexander Gonzalez $295,000 $60,000 $12,000
San Bernardino Albert K. Karnig $290,000 $50,000 $12,000
San Diego Stephen L. Weber $299,435 provided $12,000
San Francisco Robert A. Corrigan $298,749 $60,000 $12,000
San Jose Don W. Kassing $328,209 provided $12,000
San Luis Obispo Jeffrey D. Armstrong $350,000 $60,000 $12,000
San Marcos Karen S. Haynes $270,568 $60,000 $12,000
Sonoma Ruben Arminaña $291,179 $60,000 $12,000
Stanislaus Hamid Shirvani $270,000 $50,000 $12,000
請告訴我們錯誤信息,它發生在哪裏 –
行了,你從哪裏得到的出界失誤?堆棧跟蹤請求... – Sweeper
'String line = in.nextLine(); String [] lineArray = in.nextLine()。split(「\\ s +」);'你確定這是正確的嗎?此時您正在跳兩行。用'line'替換第二個'in.nextLine()'。因此,如果在你的文件中的行號,甚至......它將會崩潰(甚至代替奇怪,因爲在循環開始之前一行跳過) –