0

Pretty much the program I am writing has a 'Usage' method which gets called if args[0] contains nothing. Here is the program, When ever I try something of the sort such as (args[0].isEmpty()...) I get an ArrayIndexOutOfBounds.

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;


public class Desk {

public static void main(String[] args) throws FileNotFoundException {   
    if(args[0].isEmpty()){
        Usuage();
    }

    try{
        count(new TreeSet<String>(),  new File("C:\\Users\\Ceri\\workspace1\\Cw2Task2\\src\\" + args[0]));
    }catch(FileNotFoundException e){
        System.out.println("Error: File not found");
    }

}

private static void count(TreeSet<String> treeSet, File file) throws FileNotFoundException {
    // TODO Auto-generated method stub
    Scanner in = new Scanner(file);

    while(in.hasNext()){
        String temp = in.next();
        treeSet.add(temp);
    }

    System.out.println("There are " +  treeSet.size() + "Unique words in the text file ");  
}

private static void Usuage(){
        System.out.println("Not entered");
}

}
Z-2
  • 73
  • 1
  • 8

1 Answers1

3

You should check :

if(args.length < 1) {
Eran
  • 387,369
  • 54
  • 702
  • 768