-4

this is my first try at java and I'm having a little problem running it:

Here's the code:

public class Assignment1 {
    public static void main(String args[]) {
        int c = Integer.parseInt(args[1]);
        if (c > args[0].length()) {
            System.out.println("the index" + args[1] + "is out of range !");
        } else {
            System.out.println("The character is" + args[0].charAt(c - 1) + " !");
        }
    }
}

I get an error compiling it (Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1 at Assignment1.main(Assignment1.java:3) But thought it was just because I didn't input anything, and when trying to execute it in cmd "c:\ blah blah...\ java Assignment1.java 1234 5" and got the error mentioned before...

Any idea about what's wrong with the code? my settings? me? thanks.

Thanga
  • 7,811
  • 3
  • 19
  • 38
user114138
  • 143
  • 7
  • 3
    *"I get an error compiling it"* I highly doubt that. Post _how_ you try to compile your program, please. – Tom Mar 10 '16 at 10:42
  • And where? Which IDE do you use? – Tom Mar 10 '16 at 10:45
  • How do you have an error compiling it, and a 'could not find main class' error when executing it, and a runtime error? – beresfordt Mar 10 '16 at 10:50
  • @Tom I'm using Eclipse 4.5.2 and jdk 1.8.0_73 – user114138 Mar 10 '16 at 10:53
  • @beresfordt don't really know how to answer that. I just wrote the program, CTRL+F11 gave me an error and cmd too- as i've said I'm new to this – user114138 Mar 10 '16 at 10:55
  • 1
    Ctrl+F11 launches the program, so how do you know that the problem is at compile time? (in fact, it isn't) – Tom Mar 10 '16 at 11:01
  • @Tom I succeeded executing my code via eclipse, the cmd still gives me an error. so my mistake was with defining something wrong? – user114138 Mar 10 '16 at 11:03
  • @user114138 In command line you have to run `javac Assignment1.java` first. Then you have to execute `java Assignment1` – Thanga Mar 10 '16 at 11:04
  • And what does the "cmd" say? And how do you call it? Btw: please check how to use the `java` command. You need to call the compiled code, not the source code. – Tom Mar 10 '16 at 11:05

2 Answers2

2

The problem is in args[0].charAt(c-1) because c-1 is 4.

And your args[0] is 1234, so charAt(4) is out of bounds.

  • charAt(0) returns 1
  • charAt(1) returns 2
  • charAt(2) returns 3
  • charAt(3) returns 4
Martin Tournoij
  • 26,737
  • 24
  • 105
  • 146
jonhid
  • 2,075
  • 1
  • 11
  • 18
1

You're getting an ArrayIndexOutOfBoundsException because you're accessing the command line arguments without checking that there actually are any.

You need to check whether the user provided the proper arguments, and if not, print a message how to call the executable as below

public class Assignment1 {
    public static void main(String args[]) {
        if (args.length == 2) {
            int c = Integer.parseInt(args[1]);
            if (c > args[0].length()) {
                System.out.println("the index " + args[1] + " is out of range !");
            } else {
                System.out.println("The character is " + args[0].charAt(c - 1) + " !");
            }
        }
        else {
            System.out.println("Usage: java Assignment1 <argument1> <argument2>\n E-g java Assignment1 abcd 4");
        }
    }
}

When debugging, you can pass command line arguments to your application like this:

In command line you have to run

javac Assignment1.java 

first. Then you have to execute

java Assignment1 <argument1> <argument2>

Or in Eclipse IDE, Open Run Configuration window and give two arguments in the Program Arguments box. Then Run it. This will work

enter image description here

Thanga
  • 7,811
  • 3
  • 19
  • 38