0

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();
        }

    }
}
Raed Tabani
  • 217
  • 3
  • 7

1 Answers1

-1

If you want to compare Strings against each other, use String#equals(String). For instance

if ("E".equals(key))

See this Stackoverflow question as a reference.

Basically, if you compare instances using ==, you just compare the reference value and not the content. Using ==, two strings don't just have to contain the same string, they have to be the same object. The String#equals(String) method is implemented so that it just compares the content of the string instance (which is what you want).

You can also use String#equalsIgnoreCase(String) if you want the comparison to be case insensitive.

Community
  • 1
  • 1
brimborium
  • 9,362
  • 9
  • 48
  • 76
  • this worked! thanks man now I have a functional piano player, all I have to do now is figure out how to make the spaces in sheetcode count so it produces the right rythem :) – Raed Tabani Feb 27 '15 at 18:39
  • Glad I could help. Also for the person who downvoted: It is ok to downvote, but I would appreciate if you would tell me why, so I can improve this and future answers. :) – brimborium Feb 28 '15 at 10:01