String[ ] to int[ ] constructor help

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.
 
M

Mark Voorberg

Hi Chris,

The first thing you need to do is declare a new array in each of your
constructors.
You can use the length of the input array to define the length of the new
array.

// Declare a new array with a length the same as your input param.
array = new int[tileNums.length];

Then change your "for" loops to use this size as well so your program can be
more dynamic.
i < tileNums.length

I don't really follow where you got the null pointer, but remember that
after declaring your array like this:
private int[] array;
....it is still null. You need to define the int array that it will contain
like this:
array = new int[123]; // Defined with a length of 123 ints.

You do realize of course that I wrote the class so that I could answer your
questions but if I posted it here you wouldn't actually learn anything. ;)

Good Luck!
Mark
www.ObjectClarity.com



perpetuallyfrozen said:
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.
 
R

Rob

public class PuzzleState {
private int[] array;

public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {
array[0] = Integer.parseInt(tileNums[0]);

Will this help?

for(int i = 0; i < tileNums.length; i++){
array = Integer.parseInt(tileNums);
}

-Rob
 
J

Jonathan Mcdougall

I can get my code to compile, but when I execute it, I get an
ArrayOutOfBoundsException.

Surprising, since you should get a NullPointerException here, since
array is a null reference.
public class PuzzleState {
private int[] array;

Here you have a reference to a int[] which is null. There is no array
in memory. Something like

int *array;
public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {
array[0] = Integer.parseInt(tileNums[0]);

Three things. First, you are trying to use a null reference. Second,
you put 0 instead of 'i'. Third, imagine if tileNums contains only 8
elements.

Try to add

array = new int[tileNums.length];

at the top of the ctor and replacing the magic numbers.
}
}

public PuzzleState(int[] tileNums) {
for (int i=0; i<9; i++) {
array = tileNums;
}
}


Same thing here.
public void display() {...}
}

In Java, every 'variable' is like a C++ pointer. It can be null and
must point to a valid memory address. When, in C++, you do

int main()
{
int *array = new int[10];
// ..
delete array;
}

you must do in Java

public static void main()
{
int []array = new int[10];
// ...
}

What you did is something like

int main()
{
int *array;
array[0] = 10;
}

which dereferences an invalid pointer, resulting in undefined
behavior.

Jonathan
 
B

Brad BARCLAY

perpetuallyfrozen said:
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.

You're running into problems because your code is riddled with errors.
Where to start...
public class PuzzleState {
private int[] array;

public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {

Okay -- first problem -- you're hard-coding the size of the array at 9.
Don't do this. Your "for" loop initializer should look like this:

for (int i=0;i<tileNums.length;i++) {

In this way, your constructor can process arrays of _any_ size.
array[0] = Integer.parseInt(tileNums[0]);

This line contains three problems that I can see:

1) You haven't initialized "array" at all. You've told the compiler
what "array" is, but you haven't actually reserved any storage for it.
You should have this line _prior- to the initialization of your for loop:

array = new int[tileNums.length];

2) Each pass through the loop you're setting the result of the
Integer.parseInt() call into array index 0, obliterating the previous
result in the process, and using none of the other array cells. You
should be assigning to "array".

3) Like #2 above, you're always reading the input for
Integer.parseInt() from array cell 0 of your input array. The entire
line should read:

array = Integer.parseInt(tileNums);

The entire constructor is going to cause you problems, because you're
not catching (or passing on) NumberFormatException. You have the option
of either handling the error within the constructor in a manner that
suits you (print an error, use a default value, exit the program, etc.),
or you can simply rethrow the exception and let the caller deal with it.
I'll re-write your method for the latter:

public PuzzleState(String[] tileNums)
throws NumberFormatException {
array = new int[tileNums.length];
for (int i=0; i<tileNums.length; i++) {
array = Integer.parseInt(tileNums);
}
}

Next, the int[]-accepting constructor. It suffers from the for loop
problem mentioned above, as well as the array instantiation issue
mentioned above. Note that you did correctly iterate inside the loop
this time. It should be re-written as:

public PuzzleState(int[] tileNums) {
array = new int[tileNums.length];
for (int i=0; i<tileNums.length; i++) {
array = tileNums;
}
}

Everything else looks okay.

HTH!

Brad BARCLAY
 
P

perpetuallyfrozen

Brad BARCLAY said:
2) Each pass through the loop you're setting the result of the
Integer.parseInt() call into array index 0, obliterating the previous
result in the process, and using none of the other array cells. You
should be assigning to "array".


Sorry about that, the zeros should have been i's, not that it matters
now. I was attempting to locate my problem, but i forgot to replace
the zeros for my post.
3) Like #2 above, you're always reading the input for
Integer.parseInt() from array cell 0 of your input array. The entire
line should read:

array = Integer.parseInt(tileNums);

The entire constructor is going to cause you problems, because you're
not catching (or passing on) NumberFormatException. You have the option
of either handling the error within the constructor in a manner that
suits you (print an error, use a default value, exit the program, etc.),
or you can simply rethrow the exception and let the caller deal with it.
I'll re-write your method for the latter:

public PuzzleState(String[] tileNums)
throws NumberFormatException {
array = new int[tileNums.length];
for (int i=0; i<tileNums.length; i++) {
array = Integer.parseInt(tileNums);
}
}

Next, the int[]-accepting constructor. It suffers from the for loop
problem mentioned above, as well as the array instantiation issue
mentioned above. Note that you did correctly iterate inside the loop
this time. It should be re-written as:

public PuzzleState(int[] tileNums) {
array = new int[tileNums.length];
for (int i=0; i<tileNums.length; i++) {
array = tileNums;
}
}


Thanks for the help! I figured my main problem had something to do
with the size of the array. I couldn't figure out how to set it in
java, but now I think I understand. I would like to thank everyone
that replied.

- Chris R.
 
?

=?ISO-8859-1?Q?Andree_Gro=DFe?=

perpetuallyfrozen said:
..
public class PuzzleState {
private int[] array;

public PuzzleState(String[] tileNums) {
for (int i=0; i<9; i++) {
array[0] = Integer.parseInt(tileNums[0]);
}
}

Make it like that:

public PuzzleState(String[] tileNums) {
int len = tileNums.length;
array = new int[len];
for (int i = 0; i < len; i++) {
array = Integer.parseInt(tileNums);
}
}

A.G.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top