2014-02-12 45 views
0

現在的問題是我得到java.lang.ArrayIndexOutOfBoundsException:「java.lang.ArrayIndexOutOfBoundsException」問題與陣列邏輯

我一直在擺弄周圍的幾個小時,不知道問題是否在SalesManagerSalesAnaylzer

我所試圖做的是格式化數組值的錢,並添加標貼店1 ..和QTR 1 ... Im相當肯定我得到了totalSaleshighestStoreSalesaverageStoreSales正確 這裏是我的代碼:

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) 
+1

嘗試用實際的文件路徑替換'fileName'變量,看看是否有效。 – helderdarocha

+0

嗯,我需要用戶輸入文件名,所以fileName永遠不會是相同的 – user2985542

+0

fileName是否有空格嗎? – BroSlow

回答

1

我猜你正在使用的文件名與它的內部空間。 scan.next()將採用由空格分隔的下一個「標記」。如果您的文件名是例如:C:\User\John Smith\Documents\file.ext,那麼fileName變量將只包含C:\User\John。取而代之的是,使用scan.nextLine()來獲取一切,直至Enter密鑰。

除非你用它發佈你的完整堆棧跟蹤(從異常輸出的每一行錯誤輸出),否則我們不能確切地看到問題是什麼,所以我要再次猜測問題可能是什麼。

您可能正在使用沒有完整路徑的文件名,程序也不知道在哪裏可以找到它。

  • 如果您使用的是IDE,該文件應位於同一個目錄中,即「src」和「classes」文件夾。
  • 如果您使用的是命令行,請確保該文件位於您的「類路徑」中。這是JVM查找沒有完整路徑的文件名的地方。這通常與您執行的文件.class.jar位置相同。

我想不出任何其他可能與您的代碼有關的問題,如果我沒有提到任何問題修復了您的問題,請編輯您的主帖以包含完整的堆棧跟蹤。

編輯︰從這個答案的評論主題,我們得出結論,用戶遇到的問題是,他們從文件名中排除文件擴展名。 CompuDataSales應該是CompuDataSales.txt。其他有類似問題的用戶應該檢查以確保擴展名存在於文件名中。

+0

文件的名稱是:CompuDataSales so theres no space in it – user2985542

+0

該文件是否具有擴展名,如'.txt'或'.doc'?你在Windows上,你應該修改你的文件夾選項以隨時顯示文件擴展名。 – Axoren

+0

是它的一個.txt文件,我甚至分配了用戶輸入,並且自己調用了這個文件並得到了相同的結果 – user2985542