Click to See Complete Forum and Search --> : Java error?
bobtcowboy
11-14-2000, 07:24 PM
Hey guys... i've been working on a project for my CS class... the code compiles fine but it gives me an error when I run it :
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at Lab6.main(Compiled Code)
can ya possibly tell me what I'm doing wrong?
import sdsu.io.*;
/* I'm sure that all parts that have to do with this are working properly... all I'm using it for is the "Console.ReadInt();" line
*/
public class Lab6
{
public static void main(String args[])
{
System.out.println("Enter an odd number between 1 and 19:");
int N = Console.readInt();
N = (N-1);
int row = 0;
int column = 0;
int magic_number = 1;
int[][] array = new int[N][N];
do
{
if (array[row][column] == 49)
System.out.println("blah"); // this will be where I print the array when I figure out how;
else if ((array[row][column]) != 0)
row++;
else
if (row > N)
row = 0;
else if (column == -1)
column = N;
else
array[row][column] = magic_number;
magic_number++;
row++;
column--;
}
while (magic_number <= 49);
}
}
BobjoB
11-14-2000, 07:34 PM
its been a while for me in java but i believe the error is in your last else.
the column-- statement is the error.
tracing the login of column is initialized to 0 and is never modified the program goes down to the last else statement and sets it to -1, then continues the loop. when you try to access the array with the column value as an index you get your error, hope i helped
bobtcowboy
11-14-2000, 11:10 PM
Ok... back again ;o)
Alright... I made a few changes to it and I'm debugging... this time I don't get an error but I know I'm doing something wrong... in the end I get an answer of "Working823544" (if I input 7 in the beginning) Now... the output is supposed to be a magic square of N x N... if I input 7, the final value of $magic_number should be 49... but as you can see, at the end its 823544... whoa. Where is this uber looping happening? I've been staring at this same code all day... *sigh* thanks for any and all help guys
PS: this assignment is past due, but I still wanna know whats going on.
import sdsu.io.*;
public class Lab6
{
public static void main(String args[])
{
System.out.println("Enter an odd number between 1 and 19:");
int N = Console.readInt();
N = (N-1);
int row = 0;
int column = (N/2);
int magic_number = 1;
int[][] array = new int[N][N];
do
{
if ((row == -1) | | (column > N))
if (row == -1)
row = N;
else if (column > N)
column = 0;
else if (array[row][column] == (Math.pow((N+1),(N+1))))
for (row = 0; row <= 6; row++)
{
for (column = 0; column <= 6; column++)
{
System.out.println(array[row][column]);
}
}
else if ((array[row][column]) != 0)
row++;
else
array[row][column] = magic_number;
magic_number++;
row--;
column++;
}
while (magic_number <= (Math.pow((N+1),(N+1))));
System.out.println("working" + magic_number);
}
}
Paul Weaver
11-14-2000, 11:19 PM
else
array[row][column] = magic_number;
magic_number++;
row--;
column++;
Your indentation doesnt add up. It looks like you want
else {
...
...
...
}
as it is its
else {array[row][column] = magic_number;}
// rest regardless of if ((row == -1) | | (column > N))
magic_number++;
[...]
I'm guessing you want the one with braces.
Paul Weaver
11-14-2000, 11:20 PM
You'll need braces arround the nested else that last else was part of.
Do yourself a favour and put braces on all your if's, else's and whiles etc.
Paul Weaver
11-14-2000, 11:28 PM
Here's my take on the code
<snip code>
Lets see how that comes out!
[edit]
Not very well - no carrage returns!!
I'd email it you If I could. I've uploaded it to http://www.isorox.co.uk/code.txt
You'll need to strip CR to use in some programs, as I'm stuck in windows until I reboot!
[This message has been edited by Paul Weaver (edited 14 November 2000).]
Paul Weaver
11-14-2000, 11:36 PM
Out of interest, what is a "magic square"
bobtcowboy
11-15-2000, 12:18 AM
Paul,
Thanks ever so much for the ideas, I'll try that out in a few minutes, here...
A magic square is an inane more or less useless bit of math... basically it works like this (at least, the magic squares that this program is supposed to generate)...
Take an odd number. We'll take '3' for simplicity's sake. basically, the number you choose, "N", is the length of the sides of the square... you put the number "1" in the middle of the top row (which is why even numbers won't work in this case) and proceed to place the next number (2) one row up and one column to the right of the previous. Since this isn't inside the square, you instead move the number 2 one column over, but to the bottom. If a number already occupies the space you are about to put a number in, just drop down one row from where you are right now... so an N = 3 square would look like this
8 1 6
3 5 7
4 9 2
follow? and thats just one type of magic square... there's a ton of different ways, I guess... anyways... my program is supposed to ask for a number, N, and fill up an NxN array (where N is an odd number from 1 to 19; inclusive) and then print it out like it would look above...
oh... another thing about magic squares (and I believe this applies to all variations of them) they add up to the same thing horizontally, diagonally, and vertically: ((N^3 + N)/2) so 8 + 1 + 6 = 15 and 4 + 5 + 6 = 15 as well... and so on and so on
Dru Lee Parsec
11-15-2000, 01:46 AM
OK, Here's a bit of bizzare history from my life.
For 2 years I made my living as a professional magician.
There, I've admitted it. Please don't hate me, I'm much better now.
In any case, I did an illusion with a magic square. I would have a person write a number between their age and their weight on a piece of paper. That would help them visualize their number. We would then tear the paper to shreds and burn it (without ever reading or saying aloud the number).
I would then draw a 4x4 grid and tell them that I have 16 chances to read their mind and guess the number. If they see the number they should yell STOP!
I would start filling the square with numbers waiting for them to say STOP. They never would and it would appear that I messed up the trick.
I'd ask what their number was and they would tell me. I would then show them that each row and each column added up to the number they were thinking of. Also each diagonal added up to their number. The 4 corners added up to their number as did each group of 4 in each corner. Even the 4 center squares added up to their number.
Cool effect but you gotta do some serious math in your head while you're talking.
So yeah, Magic squares are cool.