-2
 public void  ZeichneFigur(String position) {
    String[] parts = position.split("\\.");
    String part0 = parts[0];
    String part1 = parts[1];
    int k = Integer.valueOf(part1);
    if (part0.equals("gr"))
        g.setColor(Color.GREEN); }

         switch (k) {        
         case 1: 
             g.fillOval(20,20,100,100);
              Break;
         case 2:
             g.fillOval(20,30,100,100);
              Break;
         case 3: 
             g.fillOval(20,40,100,100);
              Break;
         case 4:
             g.fillOval(20,50,100,100);
              Break;
         case 5: 
             g.fillOval(20,60,100,100); Break;}

First of all let me excuse my barbaric coding im a beginner. Im trying to write a short Method that takes strings of the format gr.2 that define values of the players color and the field that he is on, then paints ovals on the corresponding position. (Actually i have 40 cases, they are all the same). When i compile i get the ArrayINdexOutOfBoundsException, and i cant find out why, i understand that this means my index is out of range, but how does that happen when i enter gr.1 ?

EDIT: Changed if (part0.equals("gr")) and added break statements.

1nzlinger
  • 7
  • 2
  • I wrote a short [java debugging tutorial](http://keysersblog.wordpress.com/2014/04/21/debugging-java-code-a-beginners-guide/) that you might be interested in. I use ArrayIndexOutOfBoundsException as an example. It suggests that you have some patience, but if you don't I'm guessing your problem is `part0` or `part1`. Either way, printing some stuff will surely help. – keyser Dec 26 '14 at 19:31
  • Don't test `String` equality with `==`. – Elliott Frisch Dec 26 '14 at 19:34
  • 1
    @kocko That wasn't the main problem here. I agree that it's a duplicate, it's just not the correct one. – keyser Dec 26 '14 at 19:38
  • can you point me to the other question then please? – 1nzlinger Dec 26 '14 at 19:43
  • And make sure to add a break statement after each branch in a switch statement – laune Dec 26 '14 at 19:44
  • If you aren't sure whether it is "Gr.1" or "gr.1" (as your code and your comments suggest) then use `"gr".equalsIgnoreCase( part0 )` – laune Dec 26 '14 at 19:46
  • i think there is some problem on split (may be in data) – Prashant Dec 26 '14 at 19:57

1 Answers1

0

The only possibility to get such Exception through the code you wrote is that the parts[] array has only one element, which means that there is no "." in the String position, which means there is something wrong in the way you pass the String to the method(the string passed is not what you entered), try to print the position to see what is it's form.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118