【问题标题】:"java.lang.ArrayIndexOutOfBoundsException" Issue with array logic数组逻辑的“java.lang.ArrayIndexOutOfBoundsException”问题
【发布时间】:2014-02-12 02:46:32
【问题描述】:

现在的问题是我得到 java.lang.ArrayIndexOutOfBoundsException:

我已经折腾了几个小时,不知道问题出在SalesManager 还是SalesAnaylzer

我要做的是将数组值格式化为货币并添加标签 Store 1.. 和 QTR 1... 我很确定我得到了 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)

【问题讨论】:

  • 尝试将fileName 变量替换为实际的文件路径,看看是否可行。
  • 好吧,我需要用户输入文件名,所以文件名永远不会相同
  • 文件名有空格吗?
  • 但是文件应该放在哪里?在某个目录中?您的代码似乎正在从工作目录读取文件,除非它包含完整路径。您可以使用new File(directory, filename) 并拥有一个您知道该文件所在的目录。
  • 是的,文件在同一目录中

标签: java arrays file class methods


【解决方案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。有类似问题的其他用户应检查以确保文件名中存在扩展名。

【讨论】:

  • 文件名是:CompuDataSales,所以里面没有空格
  • 文件是否有扩展名,例如.txt.doc?您在 Windows 上,您应该修改文件夹选项以始终显示文件扩展名。
  • 是的,它是一个 .txt 文件,我什至注释掉了用户输入,只是自己调用了该文件并得到了相同的结果
  • 文件名应该是CompuDataSales.txt,而不仅仅是CompuDataSales。扩展名虽然默认隐藏在 Windows 上,但仍然是文件全名的一部分。
  • 你说你输入了CompuDataSales,但后来又说是.txt。不清楚,抱歉。你在使用 IDE 吗?
猜你喜欢
  • 1970-01-01
  • 2013-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-13
  • 1970-01-01
相关资源
最近更新 更多