I'm trying to reorder a 2D array with for loops. The first generateSong() method creates the array with random doubles and works fine. Then I have the simulateSong() method. Its purpose is to take the rows from the generateSong() array and reprint them as columns, starting with the bottom one.
import java.util.concurrent.ThreadLocalRandom;
public class Guitar {
private int strings;
private int chords;
private double[][] song;
public Guitar(int mstrings, int mchords) {
this.strings = mstrings;
this.chords = mchords;
song = new double[mstrings][mchords];
}
public void generateSong() {
for (int i = 0; i < song.length; i++) {
for (int j = 0; j < song[i].length; j++) {
song[i][j] = ThreadLocalRandom.current().nextDouble(27.5, 4186);
System.out.printf(" %.2f",song[i][j]);
}
System.out.println();
}
}
public void simulateSong() throws InterruptedException {
System.out.println("\nGuitar.simualateSong() ");
for(int i = song.length-1; i >= 0; i--) {
for(int j = song[i].length-1; j >= 0; j--) {
song[i][j] = song[i][0];
System.out.printf(" %.2f",song[i][j]);
}
System.out.println();
}
}
}
The number of rows and columns are set by command line arguments in the main method.
public class Songwriter {
public static void main(String[] args) throws InterruptedException {
System.out.println("Guitar(): Generated new guitar with " + args[0] + " strings. Song length is " + args[1] + " chords.");
String args0 = args[0];
int strings = Integer.parseInt(args0);
String args1 = args[1];
int chords = Integer.parseInt(args1);
Guitar guitarObj1 = new Guitar(strings, chords);
guitarObj1.generateSong();
guitarObj1.simulateSong();
}
}
So ultimately what I'm trying to do is make it so that the rows originally read left to right are now read as columns from top to bottom. Here's the intended output with 3 rows and 4 columns set as command line arguments.
Guitar(): Generated new guitar with 3 strings. Song length is 4 chords.
2538.83 2269.30 1128.09 3419.77
2356.74 2530.88 2466.83 3025.77
3898.32 3804.22 3613.94 337.93
Guitar.simualateSong()
3898.32 2356.74 2538.83
3804.22 2530.88 2269.30
3613.94 2466.83 1128.09
337.93 3025.77 3419.77
And with the code that I currently have this is the output I'm getting.
Guitar.simualateSong()
3898.32 3898.32 3898.32 3898.32
2356.74 2356.74 2356.74 2356.74
2538.83 2538.83 2538.83 2538.83
I know that the only problem(s) lie in the for loops of the simulateSong() method. As you can see my output is close, but no cigar.