Reputation: 3
When i try to run this code I keep getting this error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Oblig3B.main(Oblig3B.java:8)
Here is my code:
import java.util.*;
import java.io.*;
class Oblig3B{
public static void main(String[]args){
OrdAnalyse oa = new OrdAnalyse();
Line 8. String filArgs=args[0];
oa.analyseMetode(filArgs);
}
}
class OrdAnalyse{
void analyseMetode(String filArgs){
Scanner input, innfil;
String[] ord;
int[] antall;
int antUnikeOrd, antOrd;
PrintWriter utfil;
boolean sjekk;
input=new Scanner(System.in);
ord=new String[5000];
antall=new int[5000];
antUnikeOrd=0;
antOrd=0;
sjekk=true;
try{
innfil=new Scanner(new File(filArgs));
//Here it reads the file, word by word.
while(innfil.hasNext()){
String ordLest=innfil.next().toLowerCase();
sjekk=false;
for(int i=0; i<ord.length; i++){
if(ordLest.equals(ord[i])){
antall[i]+=1;
sjekk=true;
}
}
if(!sjekk){
ord[antUnikeOrd]=ordLest;
antall[antUnikeOrd]++;
antUnikeOrd++;
}
antOrd++;
}
innfil.close();
}catch(Exception e){
e.printStackTrace();
}
try{
utfil=new PrintWriter(new File("Oppsummering.txt"));
utfil.println("Antall ord lest: " +antOrd+ " og antall unike ord: "+antUnikeOrd+"
"+ ord.length);
finnOrd(antall, ord, utfil);
for(int i=0; i<ord.length; i++){
utfil.println(ord[i]+(" ")+antall[i]);
}
utfil.close();
}catch(Exception e){
e.printStackTrace();
}
}
void finnOrd(int[] antall, String[] ord, PrintWriter utfil){
int teller=1000;
for(int i=0; i<ord.length; i++){
if(antall[i]>teller){
teller=antall[i];
}
double tiprosent=teller*0.90;
System.out.println(tiprosent + " " + teller);
for(i=0; i<ord.length; i++){
if(antall[i]>tiprosent){
utfil.println("Vanlige ord: "+ord[i]+"t("+antall[i]+" forekomster)");
}
}
}
}
}
I'm not sure what I need to do to fix this error. I would appreciate any help I can get.
(I'm sorry if the code is a bit messy, not sure how I can fix it properly here on stackoverflow)
Thanks alot
Upvotes: 0
Views: 564
Reputation: 2155
If you are running your program via command line you can pass one more parameter while running the command. If you are using an IDE e.g. eclipse you can pass the arguments by setting it via Properties -> Run/Debug settings
Basically, String filArgs=args[0];
is looking for first argument which doesn't exists
Upvotes: 1
Reputation: 178253
You didn't pass any command line arguments, so the args
array is of zero-length. So, attempting any array access will throw an ArrayIndexOutOfBoundsException
.
Test the length of the args
array before attempting to access it. Here, make sure that the length is at least one before accessing args[0]
. Something like:
if (args.length >= 1)
{
String filArgs=args[0];
// Do your processing.
}
else
{
// Handle the error here.
}
Upvotes: 1
Reputation: 279890
Java binds each argument passed to the application launched as an array to the String[] parameter in your
main` method.
If you don't pass any arguments to the application launcher, for instance like
java Oblig3B
then the array bound will have size 0 and therefore
String filArgs = args[0];
trying to access the first element, at index 0, will fail. Check that you are actually passing arguments.
java Oblig3B "Some agument"
Upvotes: 1