Question
In JAVA: When I try to run my program I get this: Exception in thread main java.lang.ArrayIndexOutOfBoundsException: 0 at accidents.main(accidents.java:15) I have provided the assignment
In JAVA: When I try to run my program I get this:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at accidents.main(accidents.java:15)
I have provided the assignment instructions in the last two photos. I do not know what is wrong with my code. Please help.
import java.util.*; import java.io.*;
public class accidents {
public static void main (String args[]) throws FileNotFoundException{
Scanner scan = new Scanner(new File(args[0]));
int counter =0; //keep track of how many integers in the file
while(scan.hasNextInt())
{
counter++;
scan.nextInt();
}
Scanner scan2 = new Scanner(new File(args[0]));
int hours,vehicles,fatals;
hours=counter/3;
vehicles=counter/3;
fatals=counter/3;
int h[] = new int[hours];
int v[] = new int[vehicles];
int f[] = new int[fatals];
for(int i=0;i { h[i]=scan2.nextInt(); v[i]=scan2.nextInt(); f[i]=scan2.nextInt(); } int ma=0,aa=0,na=0,mv=0,av=0,nv=0,mf=0,af=0,nf=0; for(int i=0;i { if(h[i]>=6 && h[i] { ma++; mv+=v[i]; mf+=f[i]; } else if(h[i]>=13 && h[i] { aa++; av+=v[i]; af+=f[i]; } else { na++; nv+=v[i]; nf+=f[i]; } } System.out.println("Time Span Accidents Vehicles Fatals Vehicle/Accid"); System.out.println("---------------------------------------------------------------------------"); String s[]={"Morning","Afternoon","Night"}; String t=s[0]; double l=1,l1; if(ma!=0) { l=(double)mv/ma; System.out.println(s[0]+" "+ ma+ " \t \t "+ mv+" \t\t "+mf+" "+l); } else System.out.println("No Accidents in the Morning"); System.out.println(); if(aa!=0) { l1=(double)av/aa; if(l1>l) { t=s[1]; l=l1; } System.out.println(s[1]+" "+ aa+ " \t \t "+ av+" \t "+af+" "+l1); } else System.out.println("No Accidents in the Afternoon"); System.out.println(); if(na!=0) { l1=(double)nva; if(l1>l) { t=s[2]; l=l1; } System.out.println(s[2]+" "+ na+ " \t \t "+ nv+" \t "+nf+" "+l1); System.out.println(); } else System.out.println("No Accidents in the Night"); System.out.println(); System.out.println(); System.out.println(); System.out.println("The "+t+" time has the highest vehicle to accident ratio which means more vehicles are involved in each accident in the "+t+" than any other times"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started