P
perpetuallyfrozen
Hello, I am working on a project that requires me to code in two
constructors (among other things) with java. One of the constructors
is meant to convert a String array into an array of numbers (using
Integer.parseInt()), and the other is for an array of int's. Here are
the bodyless definitions:
public PuzzleState(String[] tileNums) {...}
public PuzzleState(int[] tileNums) {...}
I can get my code to compile, but when I execute it, I get an
ArrayOutOfBoundsException. Also, when I comment out everything
concerning the String->int constructor to test the other constructor,
I get a NullPointerException. I have spent hours today and yesterday
attempting to figure out what I am doing wrong, but I can't come up
with an answer. I am not used to java, as all of my past work was
done in c++. I am used to setting up arrays with boundaries (int[9]
array). Here is what I have so far, but I don't know if any of it is
correct.
public class PuzzleState {
private int[] array;
public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {
array[0] = Integer.parseInt(tileNums[0]);
}
}
public PuzzleState(int[] tileNums) {
for (int i=0; i<9; i++) {
array = tileNums;
}
}
public void display() {...}
}
And here is the main() for the program so far:
public class PuzzleTest1 {
/**
* This method tests the PuzzleState class. An initial state
* and final state are created using two different
constructors.
* Each state is displayed using the PuzzleState.display()
method.
* Test call: java PuzzleTest1 2 8 3 1 6 4 7 0 5
*/
public static void main(String[] args) {
PuzzleState initial_state = new PuzzleState(args);
initial_state.display();
System.out.println();
int[] final_tiles = {1, 2, 3, 8, 0, 4, 7, 6, 5};
PuzzleState final_state = new PuzzleState(final_tiles);
final_state.display();
}
}
Basically, the program is loaded with a string of numbers (in this
case "2 8 3 1 6 4 7 0 5") and I have to make the constructor put those
numbers into an array of int's. I am guessing one of my problems has
something to do with the declaration of the int[] array-
private int[] array;
-but I am sure that's not the only thing wrong. This is one of the
only topics that seems to hang me up. Any help in the matter will be
greatly appreciated, as I seem to be in a rut. I never have fully
understood constructors, and switching to java makes it just that much
more difficult for me. Thanks in advance for any suggestions.
- Chris R.
constructors (among other things) with java. One of the constructors
is meant to convert a String array into an array of numbers (using
Integer.parseInt()), and the other is for an array of int's. Here are
the bodyless definitions:
public PuzzleState(String[] tileNums) {...}
public PuzzleState(int[] tileNums) {...}
I can get my code to compile, but when I execute it, I get an
ArrayOutOfBoundsException. Also, when I comment out everything
concerning the String->int constructor to test the other constructor,
I get a NullPointerException. I have spent hours today and yesterday
attempting to figure out what I am doing wrong, but I can't come up
with an answer. I am not used to java, as all of my past work was
done in c++. I am used to setting up arrays with boundaries (int[9]
array). Here is what I have so far, but I don't know if any of it is
correct.
public class PuzzleState {
private int[] array;
public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {
array[0] = Integer.parseInt(tileNums[0]);
}
}
public PuzzleState(int[] tileNums) {
for (int i=0; i<9; i++) {
array = tileNums;
}
}
public void display() {...}
}
And here is the main() for the program so far:
public class PuzzleTest1 {
/**
* This method tests the PuzzleState class. An initial state
* and final state are created using two different
constructors.
* Each state is displayed using the PuzzleState.display()
method.
* Test call: java PuzzleTest1 2 8 3 1 6 4 7 0 5
*/
public static void main(String[] args) {
PuzzleState initial_state = new PuzzleState(args);
initial_state.display();
System.out.println();
int[] final_tiles = {1, 2, 3, 8, 0, 4, 7, 6, 5};
PuzzleState final_state = new PuzzleState(final_tiles);
final_state.display();
}
}
Basically, the program is loaded with a string of numbers (in this
case "2 8 3 1 6 4 7 0 5") and I have to make the constructor put those
numbers into an array of int's. I am guessing one of my problems has
something to do with the declaration of the int[] array-
private int[] array;
-but I am sure that's not the only thing wrong. This is one of the
only topics that seems to hang me up. Any help in the matter will be
greatly appreciated, as I seem to be in a rut. I never have fully
understood constructors, and switching to java makes it just that much
more difficult for me. Thanks in advance for any suggestions.
- Chris R.