Note that there are some explanatory texts on larger screens.

plurals
  1. POString out of bounds,
    primarykey
    data
    text
    <p>This program is to use the keyboard keys to play notes. I get a different string index out of range for each key I press, ranging from 49 for the 1 to 109 for the m. but I always get this error message. I am new to Java, and any help would be appreciated since I've checked a bunch of forums and haven't found the answer to quite this kind of problem. </p> <p>The exception is thrown at this line: </p> <pre><code>nextnote = keyboard.charAt(key); </code></pre> <p> This is my code:</p> <pre><code>public class GuitarHero { public static void main(String[] args) { //make array for strings double[] notes = new double[37]; GuitarString[] strings = new GuitarString[37]; int nextnote; int firstnote=0; double NOTE = 440.0; String keyboard ="1234567890qwertyuiopasdfghjklzxcvbnm"; //for loop to set notes for(int i=0;i&lt;37;i++){ double concert = 440.0* Math.pow(2, (i-24)/12.0); notes[i] = concert; for(int j=0;j&lt;37;j++){ strings[j] = new GuitarString(concert); } } while (true) { // check if the user has typed a key; if so, process it if (StdDraw.hasNextKeyTyped()) { char key = StdDraw.nextKeyTyped(); //charAt gets index of character in string nextnote = keyboard.charAt(key); //make sure value is within string if(nextnote&gt;=0 &amp;&amp; nextnote&lt;37){ // pluck string and compute the superposition of samples strings[nextnote].pluck(); double sample = strings[firstnote].sample() +strings[nextnote].sample(); StdAudio.play(sample); // advance the simulation of each guitar string by one step strings[nextnote].tic(); firstnote=nextnote; } } } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload