The title is supposed to be java.lang.ArrayIndexOutOfBoundsExeption: 8 not java.lang.ArrayOutOfBoundsExeption: 8
My Key class is:
My toggle class which is not mentioned in the terminal is:
and lastly, my piano (world class) is as follows:
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.
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)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();
}
}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");
}
}
}
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++;
}
}
}



