Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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");

}

}

image text in transcribed

image text in transcribed

image text in transcribedimage text in transcribed

File Edit Scurce Refatr Navigate Search Preject Rur Winduw Help Quick Access : E | [2 Package Explorer Navigator 2 A axcidents 159 f(11>1) main(String[]) : void settings > bin 161 62 InsructinParser java 163 -I classpath & projoct Hw4-Trattic Accidents Lab1-Calculaliun lab1-HalloWorild Lab1-Patterm 1-11; 65 167 169 "+ . Systrn.out . print In(s[7]+" nat \t "1 nvt" \t "enft" "t11); Systen.out.println) settings Pattem jave classpati Systen.out.println" No Accidents in the Night" 179 Syste.out.printlrn; 181 Systrm.out.printIn); 183 Systen.out.println) 135 Syste.out.pintlr"The "++ine has Lhe highesL vehicle Lo accideit ratio which neans more veicles ar 187 e Lab3-Math Method &Tvpe Conversions Lab5-Dasic Decisien Making e Lab8 While Loop Test settings CircleAreaCalculator java ProblemsJavadoc . Dedaration Corso e terminateds Testy java Application) CUsers Cod Decuments Java binyawaw exe Mar 13, 2018, 12*830 PM D Firstl ast.java Gradeinto Java lnitialsExtractor.java NameParser java 1 345 6 7 91 2 4 6810 12 14 1 1 20 369121', 18 21 24 2/ 3 12 16 24 7 32 36 4 Sun,java 5 1 15 225 30 35 40 45 50 6 12 18 2 3 36 42 48 & classpath intable Smart Insert O Type here to search 12:53 PM 3/13/2018

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mastering Real Time Analytics In Big Data A Comprehensive Guide For Everyone

Authors: Lennox Mark

1st Edition

B0CPTC9LY9, 979-8869045706

More Books

Students also viewed these Databases questions