Note that there are some explanatory texts on larger screens.

plurals
  1. POAdding random arrays to random arrays in java to make it equal another array
    primarykey
    data
    text
    <p>I am learning java, and from what I can tell, what I am looking to do is a rare situation.</p> <p>I am trying to use an API (kindof) to randomly generate musical notes. I want it to generate 20 times so i have it in a for loop. I realize that i could have used a list for this I just dont know how I could have implemented it. The question I have is, when I try to compile this code, the first part runs. It lets me make the seed. However after that it gives me</p> <pre><code>Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at co.kbcomp.music.Main.main(Main.java:64) </code></pre> <p>What could I do to prevent this? I know that what I am doing is wrong. That much I dont need to be told. What I want to know is where am I going wrong.</p> <pre><code>package co.kbcomp.music; import java.util.*; import org.jfugue.*; import java.util.Scanner; public class Main { public static void main(String[] args){ Scanner in = new Scanner(System.in); System.out.println("Please enter a number for the seed of the song"); long seed = in.nextLong(); Calendar cal = Calendar.getInstance(); Random rand; //rand = new Random(cal.getTime()); rand = new Random(seed); int NoteNumber = 0; int NoteLength = 0; int OctiveNumber = 0; int ChordNumber = 0; int InversionNumber = 0; //int Duration = rand.nextInt(100 - 5) + 5; //This keeps track of the iteration of the for loop. String[] NN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; String[] NL = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; String[] IN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; String[] CN = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; String[] ON = { " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; //This is what is being played String[] note = { "A", "B", "C", "D", "E", "F", "G" }; String[] noteLength = {"", "w", "", "h", "", "q","", "i"}; String[] octive = { "","1","", "2", "", "3", "", "4", "", "5", "", "6", "", "7", "", "8", "", "9"}; String[] chord = { "", "maj", "", "min"}; String[] inversion = {"", "^", "", "^^", "", "^^^", "", "^^^^", "", "^^^^^"}; String[] key = {"",""}; String keys= " "; String randstr = " "; //this is the loop that defines the music legnth for (int i = 0; i &lt; 21; i++) { NoteNumber = rand.nextInt(7); NoteLength = rand.nextInt(8); OctiveNumber = rand.nextInt(18); ChordNumber = rand.nextInt(4); InversionNumber = rand.nextInt(10); NN[i] = note[NoteNumber]; // This randomly generates the note to be played. NL[i] = noteLength[NoteLength]; // This randomly generates the length of the note. ON[i] = octive[OctiveNumber]; // This defines the octive to be played in. CN[i] = chord[ChordNumber]; // This is defines the major or the minor IN[i] = inversion[InversionNumber]; // IN[i] = inversion[InversionNumber]; key[i] = NN[i] + NL[i] + ON[i] + CN[i] + IN[i]; //randstr = c[0] + " " + c[1] + " " + c[2] + " " + c[3] + " " + c[4] + " " + c[5] + " " + c[6] + " " + c[7] + " " + c[8] + " " + c[9] + " " + c[10] + " " + c[11] + " " + c[12] + " " + c[13] + " " + c[14] + " " + c[15] + " " + c[16]; keys = (key[0] + " " + key[1] + " " + key[2] + " " + key[3] + " " + key[4] + " " + key[5] + " " + key[6] + " " + key[7] + " " + key[8] + " " + key[9] + " " + key[10] + " " + key[11] + " " + key[12] + " " + key[13] + " " + key[14] + " " + key[15] + " " + key[16] + " " + key[17] + " " + key[18] + " " + key[19] + " " + key[20]); } System.out.println(key); Player player = new Player(); Pattern pattern = new Pattern(key[0]); player.play(pattern); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    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