Results 1 to 3 of 3
  1. #1
    bsegvic is offline Member
    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    Default Error : Exception in thread "main" how can it be fixed?

    import java.util.HashMap;

    public class zadatak {

    public static void main(String[] args) {
    String input = args[0];

    String [] numbers = input.split("_");

    HashMap<String, Integer> frequency = new HashMap<String, Integer>();

    for(int i = 0; i < numbers.length; i++){
    if(frequency.containsKey(numbers[i])){
    Integer value = frequency.get(numbers[i]);
    value++;
    frequency.put(numbers[i], value);
    }
    else{
    frequency.put(numbers[i], 1);
    }
    }

    for(String key : frequency.keySet()){
    System.out.print(key + "_" + frequency.get(key) + " ");
    }
    }

    }

    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
    at zadatak.main(zadatak.java:6)

    After running this program I keep getting this error msg,can u please advice me, how can it be fixed?

    Thanks!

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,386
    Blog Entries
    7
    Rep Power
    20

    Default Re: Error : Exception in thread "main" how can it be fixed?

    Did you write that program yourself? If so, you must've forgotten that it needs (at least) one command line argument to run.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    AeneasTroy is offline Arma virumque cano
    Join Date
    Oct 2012
    Location
    Indianapolis
    Posts
    20
    Rep Power
    0

    Default Re: Error : Exception in thread "main" how can it be fixed?

    Yeah,

    Example

    Java Code:
    COMMAND LINE
    
    C:\Folder> myProgram.exe -x dosomthing
    In this example, "-x" and "dosomething" get stored in the args[] array. So you would have arg[0] and arg[1]

    If you were to just run myProgram.exe with no arguments, the args[] array would be empty. It wouldn't have anything at arg[0]

Similar Threads

  1. Replies: 2
    Last Post: 06-20-2012, 12:18 AM
  2. Replies: 6
    Last Post: 01-21-2012, 05:59 AM
  3. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  4. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  5. Replies: 1
    Last Post: 08-07-2007, 07:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •