当我尝试运行此代码时,我不断收到此错误:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at Oblig3B.main(Oblig3B.java:8)
这是我的代码:
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)");
}
}
}
}
}
我不确定我需要做什么来修复这个错误。我会很感激我能得到的任何帮助。
(如果代码有点乱,我很抱歉,不知道如何在stackoverflow上正确修复它)
非常感谢