現在的問題是我得到java.lang.ArrayIndexOutOfBoundsException:「java.lang.ArrayIndexOutOfBoundsException」問題與陣列邏輯
我一直在擺弄周圍的幾個小時,不知道問題是否在SalesManager
或SalesAnaylzer
。
我所試圖做的是格式化數組值的錢,並添加標貼店1 ..和QTR 1 ... Im相當肯定我得到了totalSales
,highestStoreSales
和averageStoreSales
正確 這裏是我的代碼:
import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.IOException;;
public class SalesManager
{
public static void main(String []args) throws IOException
{
System.out.println(" What is the name of the file");
Scanner scan= new Scanner(System.in);
String fileName= scan.next();
SalesAnaylzer sA = new SalesAnaylzer(fileName);
/*System.out.println(data);
System.out.println("Here are the stores' sales for the past four quarters:" +
"/n" + data);
System.out.println("The total sales were:" + total);
System.out.println("The highest quarterly sales were:" + highest);
System.out.println("The average quarterly sales were:" + avg);*/
}
}
,這裏是這個類文件:
import java.io.File;
import java.text.DecimalFormat;
import java.util.Scanner;
import java.io.IOException;
import java.util.*;
import javax.swing.*;
import java.awt.*;
public class SalesAnaylzer
{
DecimalFormat pricePattern= new DecimalFormat("$#0.00");
int [][]sales= new int [3][4];
public SalesAnaylzer(String fileName)throws IOException
{
File inputFile= new File(fileName);
Scanner scan= new Scanner(inputFile);
for (int row=0; row<4; row++){
for (int col=0; col<6; col++){
sales [row][col]= scan.nextInt();
}
}
}
public String toString()
{
String data = "";
for (int row=0; row<4; row++){
data =data +"\nStore "+(row+1)+": ";
for (int col=0; col<6; col++){
data =data + "QTR "+(col+1)+": "+pricePattern.format(sales[row][col])+" ";
}
}
return data;
}
public double totalSales()
{
double total=0.0;
for (int row=0; row<4; row++){
for (int col=0; col<6; col++){
total= total + sales[row][col];
}
}
return total;
}
public double highStoreSales(int store)
{
double highest=0.0;
for (int row=0; row<4; row++){
if(sales[row][store]> highest)
highest =sales[row][store];
}
return highest;
}
public double averageStoreSales(int quarter)
{
double total=0.0;
double avg=0.0;
for (int col=0; col<6; col++){
total=total+sales[quarter][col];
}
avg=(total/4);
return avg;
}
}
java.io.FileNotFoundException: CompuDataSales (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.util.Scanner.<init>(Unknown Source)
at SalesManager.main(SalesManager.java:16)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand(JavacCompiler.java:272)
嘗試用實際的文件路徑替換'fileName'變量,看看是否有效。 – helderdarocha
嗯,我需要用戶輸入文件名,所以fileName永遠不會是相同的 – user2985542
fileName是否有空格嗎? – BroSlow