0

I'm hoping someone can help me with these two values that have me stuck on a project. I have two classes and this first one generates a 2D array with random values.

import java.util.concurrent.ThreadLocalRandom;

public class Guitar {

private int strings;
private int chords;

public Guitar(int mstrings, int mchords) {
    this.strings = mstrings;
    this.chords = mchords;
}

private double[][] song = new double[strings][chords];

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

}

The number of rows and columns is determined by command line arguments. args[0] is the number of rows, args[1] is the number of columns. I converted them to int variables in the main method class

public class Songwriter {

public static void main(String[] args) {

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

}

}

My problem lies in passing the int variables of the command line arguments to make the 2D array the corresponding size. I know my code isn't completely wrong b/c when I set the strings and chords variables equal to 3 and 4 or whatever in the Guitar class itself, the table prints fine.

Sorry if I seem clueless. My class just covered the first chapter on object oriented programming and I've yet to get the fundamentals down.

4
  • 2
    So what actually happens? Is there some kind of issue with the output? Does it crash? Commented Apr 5, 2016 at 4:25
  • What error you are facing Commented Apr 5, 2016 at 4:29
  • It doesn't crash but the only output is the system.out.print in the first line of the main. I believe it's because the strings and chords variables default to 0, making the array 0x0, and I'm failing to change their values Commented Apr 5, 2016 at 4:30
  • As far i seen there may be two possible errors. Either indexoutofbound or numberformatexception if this is the case than you need to apply some check before executing the code. Commented Apr 5, 2016 at 4:32

1 Answer 1

1

This is the problematic line:
private double[][] song = new double[strings][chords];

When you create a new object of your Guitar class, the song array is initialized with whatever the values of strings and chords are at that time, which would (most probably) be 0.

Change it to this:

private double[][] song;

public Guitar(int mstrings, int mchords) {
    this.strings = mstrings;
    this.chords = mchords;

    song = new double[mstrings][mchords];
}

EDIT : OP you just answered your own question :)

It doesn't crash but the only output is the system.out.print in the first line of the main. I believe it's because the strings and chords variables default to 0, making the array 0x0, and I'm failing to change their values

Sign up to request clarification or add additional context in comments.

2 Comments

This is correct. Instance variable initializers always run before constructors, even if they appear textually below the constructor. Therefore, song is a double[0][0] in OP's code because int is default initialized to 0.
Thanks a ton, it works now. I knew it was 0x0 but I was stumped on fixing it. Thanks mate.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.