0

Issue now is i'm getting java.lang.ArrayIndexOutOfBoundsException:

I've been fiddling around for a few hours and don't know if the problem is in SalesManager or SalesAnaylzer.

What I am trying to do is format the array values to money and and add lables Store 1.. and QTR 1... Im pretty sure I got the totalSales, highestStoreSales, and averageStoreSales correct Here is my code:

 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);*/
  }
 }

and here is this class file:

 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)
11
  • 1
    Try replacing the fileName variable with the actual file path and see if that works. Commented Feb 12, 2014 at 2:53
  • Well i'm needing the user to input the file name so fileName is never going to be the same Commented Feb 12, 2014 at 2:54
  • Does the fileName have spaces by any chance? Commented Feb 12, 2014 at 2:57
  • But where is the file supposed to be located? In a certain directory? Your code seems to be reading a file from the working directory, unless it contains a full path. You could use new File(directory, filename) and have a directory where you know the file is located. Commented Feb 12, 2014 at 2:57
  • yes the file is in the same directory Commented Feb 12, 2014 at 3:00

1 Answer 1

1

I'm guessing you're using a filename with spaces inside of it. scan.next() takes the next "token" which is delimited by spaces. If your file name is for example: C:\User\John Smith\Documents\file.ext, then the fileName variable would only contain C:\User\John. Instead, use scan.nextLine() to grab everything up to the Enter key.

Unless you post your full stacktrace with it (every line of error output from the exception), we can't really see exactly what the issue is, so I'm going to make another guess at what the issue might be.

You may be using a filename without a full path and the program doesn't know where to find it.

  • If you're using an IDE, the file should be in the same directory are your "src" and "classes" folders.
  • If you're using the command-line, make sure that the file is in your "classpath". This is where the JVM looks for filenames that don't have full paths. This is often the same location as the .class or .jar file you're executing.

I can't think of anything else that could be wrong with your code, if nothing I mentioned fixes your issue, edit your main post to include the full stacktrace.

Edit: From the comment thread on this answer, we concluded that the issue the user was having was that they were excluding the file extension from the filename. CompuDataSales should have been CompuDataSales.txt. Other users having a similar issue should check to make sure that extensions are present in filenames.

Sign up to request clarification or add additional context in comments.

12 Comments

file name is : CompuDataSales so theres no spaces in it
Does the file have an extension, like .txt or .doc? You're on windows, you should modify your folder options to show file extensions at all times.
yes its a .txt file i even commmented out the user input and just called the file on its own and got the same result
The filename should be CompuDataSales.txt, not just CompuDataSales. The extension, though hidden on Windows by default, is still part of the full name of the file.
You said you've been putting in CompuDataSales but then said it was a .txt. It wasn't clear, sorry. Are you using an IDE?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.