0

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

5
  • How do you run your java program? Commented Oct 21, 2013 at 21:32
  • Did you pass it any arguments? I'm guessing not. Commented Oct 21, 2013 at 21:33
  • @SotiriosDelimanolis I run the java program in command prompt Commented Oct 21, 2013 at 21:34
  • This declaration looks suspicious: String filArgs=args[0]; It means your array has no elements, so you can't access any of them. Commented Oct 21, 2013 at 21:34
  • Did you try to run this program from command promt with at least one argument? Commented Oct 21, 2013 at 21:36

3 Answers 3

1

Java binds each argument passed to the application launched as an array to the String[] parameter in yourmain` 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"
Sign up to request clarification or add additional context in comments.

Comments

1

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.
}

Comments

1

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

Comments

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.