【发布时间】:2014-02-12 02:46:32
【问题描述】:
现在的问题是我得到 java.lang.ArrayIndexOutOfBoundsException:
我已经折腾了几个小时,不知道问题出在SalesManager 还是SalesAnaylzer。
我要做的是将数组值格式化为货币并添加标签 Store 1.. 和 QTR 1... 我很确定我得到了 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变量替换为实际的文件路径,看看是否可行。 -
好吧,我需要用户输入文件名,所以文件名永远不会相同
-
文件名有空格吗?
-
但是文件应该放在哪里?在某个目录中?您的代码似乎正在从工作目录读取文件,除非它包含完整路径。您可以使用
new File(directory, filename)并拥有一个您知道该文件所在的目录。 -
是的,文件在同一目录中
标签: java arrays file class methods