I'm trying to make a simple Piano sheet compiler/player, where the user feeds the program a string of piano code ex "G E F D D G...etc", which is then broken by the stringtokenizer method and stored individually in a String array called code. I've built a sheetPlayer method, which reads the array and based on multiple if statements would generate different sounds. everything seems ok but when I run the program no sound is played? and I accidentally found that if I was to directly initialize the code array ie code={"G","F"} the sound plays just find and im wondering if it's the stringtokenizer that is causing the problem?
public class PianoCompiler {
//public static Scanner reader=new Scanner(System.in);
//using input user should be able to feed the program sheet code ex"G G D E" with space in between
//and it should be store into sheetCode String
public static String sheetCode="G E G G E G A G F E D E F ";
//code Array should be able to read sheetCode String and break and store it into single letter components
public static String[] code=sheetCode.split(" ");
public static void sheetPlayer()
{
for(String key:code)
{
if(key=="G"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/g.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="F"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/f.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="E"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/e.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="D"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/d.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="C"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/c.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="B"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/b.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
if(key=="A"){
try
{
String gongFile = "/Users/Raed/Music/PianoSounds/a.wav";
InputStream in = new FileInputStream(gongFile);
AudioStream audioStream = new AudioStream(in);
AudioPlayer.player.start(audioStream);
} catch(Exception e ){
}
}
try {
Thread.sleep(3000); //1000 milliseconds is one second.
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
}