This site requires JavaScript, please enable it in your browser!
Greenfoot back
t_k_990
t_k_990 wrote ...

2018/2/24

Why does the terminal throw java.lang.ArrayOutOfBoundsException: 8?

t_k_990 t_k_990

2018/2/24

#
The title is supposed to be java.lang.ArrayIndexOutOfBoundsExeption: 8 not java.lang.ArrayOutOfBoundsExeption: 8
Greenfoot Terminal wrote...
java.lang.ArrayIndexOutOfBoundsException: 8 at Piano.displayKeys(Piano.java:40) at Piano.<init>(Piano.java:17) at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) at java.lang.reflect.Constructor.newInstance(Constructor.java:423) at greenfoot.core.Simulation.newInstance(Simulation.java:617) at greenfoot.platforms.ide.WorldHandlerDelegateIDE.lambda$instantiateNewWorld$7(WorldHandlerDelegateIDE.java:430) at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502) at greenfoot.core.Simulation.maybePause(Simulation.java:305) at greenfoot.core.Simulation.runContent(Simulation.java:218) at greenfoot.core.Simulation.run(Simulation.java:211)
My Key class is:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

public class Key extends Actor
{
    /**
     * Create a new key.
     */
    private String key;
    private String sound;
    private String blackKey;
    private String keyType;
    private String sound2;
    
    public Key(String keyName, String soundFile, String keyType, String toggle){
            key = keyName;
            sound = soundFile;
            blackKey = keyType;
            sound2 = toggle;
            if(blackKey.equals("black")) {
                setImage("black-key.png");
            }
    }
    private boolean isDown;
    private boolean isDown2;
    private void playSound() {
        if(toggle.toggled != true) {
            Greenfoot.playSound(sound);
        } else {
            Greenfoot.playSound(sound2);
        }
    }
    public void animateKey() {
        if(!blackKey.equals("black")){
        if(!isDown && Greenfoot.isKeyDown(key)){
            setImage("white-key-down.png");
            isDown = true;
            playSound();
        }
        if(isDown && !Greenfoot.isKeyDown(key)){
            setImage("white-key.png");
            isDown = false;
        }
    }else{
        if(!isDown2 && Greenfoot.isKeyDown(key)){
            setImage("black-key-down.png");
            isDown2 = true;
            playSound();
        }
        if(isDown2 && !Greenfoot.isKeyDown(key)){
            setImage("black-key.png");
            isDown2 = false;
        }
    }
    }
     public void act()  {
        animateKey();
    }
}
My toggle class which is not mentioned in the terminal is:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class toggle here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class toggle extends Actor
{
    /**
     * Act - do whatever the toggle wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        toggle();
    }    
    public static boolean toggled;
    public void toggle() {
        if(Greenfoot.isKeyDown("1") && toggled != true) {
            toggled = true;
            setImage("button-green.png");
        } 
        if(Greenfoot.isKeyDown("2") && toggled == true){
          toggled = false;
          setImage("button-red.png");
        }
    }
}
and lastly, my piano (world class) is as follows:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * A piano that can be played with the computer keyboard.
 * 
 * @author: M. Kolling
 * @version: 0.1
 */
public class Piano extends World
{
    /**
     * Make the piano.
     */
    public Piano() 
    {
        super(800, 340, 1);
        displayKeys();
        addObject( new toggle(), 761, 313);
    }
    //toggled
    //white keys
    public String[] toggledWhite = {"a.mp3", "b.mp3", "c.mp3", "d.mp3", "e.mp3", "f.mp3", "g.mp3", "h.mp3", "i.mp3", "j.mp3", "k.mp3", "l.mp3"};
    public String[] toggledBlack = {"1.mp3", "2.mp3", "3.mp3", "4.mp3", "5.mp3", "6.mp3", "7.mp3", "8.mp3"};
    //non toggled
    //White keys
    public String[] whiteKeys = {"a", "s", "d", "f", "g", "h", "j", "k", "l", ";", "'", "/"};
    public String[] whiteNotes = {"3c", "3d", "3e", "3f", "3g", "3a", "3b", "4c", "4d", "4e", "4f", "4g"};
    //Black keys
    String[] blackKeys = {"W", "E", "", "T", "Y", "U", "", "O", "P", "", "]"};
    String[] blackNotes = {"3c#", "3d#", "", "3f#", "3g#", "3a#", "", "4c#", "4d#", "", "4f#"};
    public void displayKeys() {
        int i = 0;
        while (i < whiteKeys.length) {
            Key key = new Key(whiteKeys[i], whiteNotes[i] + ".wav", "white", toggledWhite[i]);
            addObject (key, 54 + (i*key.getImage().getWidth()), 140);
            i++;
        }
        int x = 0;
        while (x < blackKeys.length) {
            Key key = new Key(blackKeys[x], blackNotes[x] + ".wav", "black", toggledBlack[x]);
            addObject (key, 85 + x*65, 85);
            x++;
           } 
        }
}
I triple checked and I am 99% sure that I did nothing wrong. In my computer science class at school, Greenfoot throws weird exceptions that have nothing to do with my code but I am not sure if that is the case this time.
Vercility Vercility

2018/2/24

#
The error already tells you where it is. Line 40, Piano, toggled Black length is 8 so index 8 doesn't exist in that array but you're going till 10 because black keys length is 11
jimboweb jimboweb

2018/2/24

#
The array where the index is going out of bounds is the toggledBlack array. There are 8 items in it, so the indexes go from 0 to 7. But there are 11 black keys so it's counting up to 11. When it calls index 8 of toggledBlackKeys it does not exist. I notice you have 3 black keys that are blank strings. I don't know anything about pianos but I assume you're doing that on purpose, because then you are left with 8 labeled black keys. If the unlabeled black keys are supposed to have unlabeled sound files your array could be like.
public String[] toggledBlack = {"1.mp3", "2.mp3", "", "3.mp3", "4.mp3", "5.mp3", "",  "6.mp3", "7.mp3", "", "8.mp3"};
Now your arrays are the same size. I have no idea if this will actually give the result you want, because I know nothing about music, but you won't get an ArrayIndexOutOfBoundsException. I'd add that making a bunch of arrays that have to be the same size is a pretty brittle solution that breaks easily as you're seeing. You'd be better off making an array of arrays that apply to the same object, or better ArrayLists of ArrayLists. It's too much to explain in this post but here are some links about how to do that: https://stackoverflow.com/questions/4781100/how-to-make-an-array-of-arrays-in-java http://www.dummies.com/programming/java/java-use-arrays-with-two-dimensions-or-more/ https://www.quora.com/Can-there-be-an-ArrayList-of-ArrayLists-If-so-how-do-I-add-elements
Yehuda Yehuda

2018/2/25

#
I'm not sure what the purpose of all the blank array parts are for. Instead of adding blanks into the toggledBlack array, the blanks in the other arrays should be removed. It happens to be that on a computer keyboard, the letter 'R' lies between the letters 'E' and 'T', but that doesn't mean you have to give it a blank index in an array. Just like you didn't for all the other keys on the board. Even if you do add in spaces into the array, you will still get errors, just in a different class. In the Key class you can't ckeck to see if a key is down if that key ("") doesn't exist. With the blank spaces in the arrays you are creating keys with the keyName not being equal to an existing key in Greenfoot.
danpost danpost

2018/2/25

#
Yehuda wrote...
I'm not sure what the purpose of all the blank array parts are for. Instead of adding blanks into the toggledBlack array, the blanks in the other arrays should be removed. It happens to be that on a computer keyboard, the letter 'R' lies between the letters 'E' and 'T', but that doesn't mean you have to give it a blank index in an array. Just like you didn't for all the other keys on the board. Even if you do add in spaces into the array, you will still get errors, just in a different class. In the Key class you can't ckeck to see if a key is down if that key ("") doesn't exist. With the blank spaces in the arrays you are creating keys with the keyName not being equal to an existing key in Greenfoot.
The blanks (empty strings) are place holders for where there is no black key on the piano keyboard. Adding blanks in the toggledBlack array will help to even out the arrays. However, a black key should not be created when an empty string is encountered in the loop within the 'displayKeys' method.
Yehuda Yehuda

2018/2/25

#
That makes sense. The whole point is that there's no key there. I now understand what's meant to be and what isn't there.
You need to login to post a reply.