-3

I want to create a switch statement to assign different audio files to statements inputted into the keyboard, which are received by the char array.

Code:

public class MainActivity extends AppCompatActivity implements View.OnClickListener {


Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = (Button) findViewById(R.id.keybutton);
    button.setOnClickListener(MainActivity.this);


}

@Override
public void onClick(View v) {
    EditText keyInput = (EditText) findViewById(R.id.key);
    String notes = keyInput.getText().toString();
    char[] charArray;
    for (char c : charArray = notes.toCharArray()) {

I Need HELP RIGHT HERE

switch ((Boolean) View.()) {/*what do i  do here?()) {
            case: 
                /*that a certain key is pressed

                 */

AND HERE

MediaPlayer mp1 = MediaPlayer.create(getApplicationContext(), R.raw.sample1);
                mp1.start();
                break;
            /* above (and/or others) happens on button click
             */

    }
    ;

    }
    ;

help?

David Doyle
  • 117
  • 6

1 Answers1

0

If your issue is the char array, don't use a char array

String notes = keyInput.getText().toString();
for (int i = 0: i < notes.length(); i++) {
    String c = ""+notes.charAt(i);
    switch (c.toLowerCase()) {
        case "a":
            // play the note 
            break;
    } 
} 

Other solutions without a switch

1) You can have a Map<Character, Integer> to store a (character, song) pairing, which is cleaner code than a massive switch statement

map.put('a', R.raw.note_a); // Name your resources based on the notes

...

char noteChar = 'a'; // For example
Integer note = map.get(noteChar);
if (note != null) {
    MediaPlayer mp = MediaPlayer.create(MainActivity.this, note);
    mp.start();
}

2) You can use dynamic resource lookups

// This returns 'R.raw.note_a', for example
int note = getResources().getIdentifier("note_"+noteChar, "raw", getPackageName()); 

if (resourceID != 0) {
    MediaPlayer mp = MediaPlayer.create(MainActivity.this, note);
    mp.start();
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • I reread this and i'm not sure this will work. based on user input, I want to create a "song" that the user "creates" based on what they put in. I want it to play on a button click. I have tried many things, with no success. I think the char array will work. I just do not know how to map it out and create a switch. I think I have to use a hash map? Can you help me in this regard? Much thanks! David – David Doyle May 25 '17 at 14:14
  • Why won't this answer work? Did you try it? I don't know what notes you have, so you'll have to add the `case` statements yourself. A map is not necessary, only "cleaner" – OneCricketeer May 25 '17 at 14:57
  • I want my case statement to include every note in the pentatonic scale. so I want it to play after the fact, based on the user input. I don't know how to write the case statement. I need in fact multiple mediaplayers to play a key that was pressed after the fact. – David Doyle May 26 '17 at 12:54
  • Learn to write a case statement, then. I don't understand your hesitance to at least try to research how – OneCricketeer May 26 '17 at 13:00
  • I've also clearly provided two alternatives that don't even require case statements – OneCricketeer May 26 '17 at 13:01
  • im just having trouble. which is the reason why I posted it on stackoverflow. I'm trying to write your code but im getting errors. – David Doyle May 26 '17 at 13:09
  • It's fine that you asked here, but you'll want to be more specific than "there's errors" or "it's not working". Try to understand, we're not running your code, this is the very first time we're seeing it. Try to write your posts as such – OneCricketeer May 26 '17 at 13:12
  • String notes = keyInput.getText().toString(); for (int i = 0: i < notes.length(); i++) { String c = ""+notes.charAt(i); switch (c) { it cant find notes.length – David Doyle May 26 '17 at 13:13
  • I don't know what you mean... I think my code is correct. That's a method, though, not a property. https://www.tutorialspoint.com/java/java_string_length.htm Maybe try learning more about Java before attempting Android? – OneCricketeer May 26 '17 at 13:16