Crazy array

• June 14th, 2010, 12:57 PM
javapenguin
Crazy array
I'm trying to make an algorithm that'll solve this puzzle:

0000
0000
0000
0000

Where you remove just six 0 s and get an even number of 0s in every row and column, though it doesn't necessarily have to be the same even number for every one.

However, my program is not working out just making an array, though perhaps a Vector might be better for this one anyway.

Code :

```import java.io.*; import java.util.*;   public class anAlgorith {   static Scanner console = new Scanner(System.in);   public static void main(String[] args) {     int i = 0; int j = 0; char[][] arrayOfOs = new char[4][4]; System.out.println(arrayOfOs.length); printArray(arrayOfOs);   }   public static void printArray(char[][] arrayOfOs) { int i, j;     for ( i = 0; i < arrayOfOs.length; i++) { for ( j = 0; j < arrayOfOs[i].length; j++)   arrayOfOs[i][j] = 'O'; System.out.println(i + "," + j);   System.out.println(j); while (j < 4) { System.out.println(arrayOfOs[i][j]); }     } } }```

Why is j being set to 4. It worked when I used a similar code in another program. Why's it going out of bounds now?
• June 14th, 2010, 01:47 PM
Norm
Re: Crazy array
Please post the full text of the error message.

Indexes go out of bounds when you set them past the end of the array.
You need to display the length of the array and the value of the index as it changes to see.
• June 15th, 2010, 03:47 AM
Faz
Re: Crazy array
This doesn't really make a difference just some general advice. Theres no need to declare i and j in the main method infact you don't need to declare them until the for loop
Code :

`for (int i = 0; i < arrayOfOs.length; i++)`

But I guess thats down to preferance but the i and j in main do nothing.

Now onto more pressing matters please indent any code you are putting on this forum it makes it much harder to read when it's not.

And what is this bit of code for?
Code :

```while (j < 4) { System.out.println(arrayOfOs[i][j]); }```
It doesn't have an iterator so it will just go into an infinite loop right?

And yes we need a bit more detail about where it's going wrong.
• June 16th, 2010, 11:56 AM
javapenguin
Re: Crazy array
Quote:

Originally Posted by Faz
This doesn't really make a difference just some general advice. Theres no need to declare i and j in the main method infact you don't need to declare them until the for loop
Code :

`for (int i = 0; i < arrayOfOs.length; i++)`

But I guess thats down to preferance but the i and j in main do nothing.

Now onto more pressing matters please indent any code you are putting on this forum it makes it much harder to read when it's not.

And what is this bit of code for?
Code :

```while (j < 4) { System.out.println(arrayOfOs[i][j]); }```
It doesn't have an iterator so it will just go into an infinite loop right?

And yes we need a bit more detail about where it's going wrong.

No, it never prints because for some odd reason it goes up to four, even though the length is 4, and I told it to be less than length.
• June 16th, 2010, 03:11 PM
Faz
Re: Crazy array
You don't have curly brackets for the second for loop so the first line after runs 4 times instead of the block you are expecting.

Code :

```for ( i = 0; i < arrayOfOs.length; i++) { for ( j = 0; j < arrayOfOs[i].length; j++) //Put bracket here arrayOfOs[i][j] = 'O';//This line runs 4 times at the moment System.out.println(i + "," + j);   System.out.println(j); while (j < 4) { System.out.println(arrayOfOs[i][j]); }     } } } //Don't forget to close it```
• June 22nd, 2010, 12:41 PM
Charlie
Re: Crazy array
Also, if you just use a while(j < 4) and then dont add to j, it will continue forever if it does enter the loop.
So I'm thinking either you're thinking of an IF or a FOR here.

I think your for-loops are a bit bonkers too perhaps. Seems you got the idea but its a bit mixed up so look over it just to make sure we understand what you want here :)
• June 23rd, 2010, 02:22 PM
javapenguin
Re: Crazy array
No, that code tells me that it is going to index 4, which doesn't exist. If it does go into an infinite loop, it means the problem is fixed. As of right now, that while keeps it from throwing an Array Out Of Bounds Exception.
• June 23rd, 2010, 03:02 PM
Charlie
Re: Crazy array
Dont get the puzzle n all, but it runs fine for me when I put {'s on the second for loop :S.

Code :

```    import java.io.*; import java.util.*;   public class Main {   static Scanner console = new Scanner(System.in);   public static void main(String[] args) {     int i = 0; int j = 0; char[][] arrayOfOs = new char[4][4];   printArray(arrayOfOs);   }   public static void printArray(char[][] arrayOfOs) { arrayOfOs[3][3] = 1; int i = 0, j = 0;   for (i = 0; i < arrayOfOs.length; i++) { for (j = 0; j < arrayOfOs[i].length; j++) {     arrayOfOs[i][j] = 'O'; System.out.println(i + ", " + j);     } } }       }```

==

run:
0, 0
0, 1
0, 2
0, 3
1, 0
1, 1
1, 2
1, 3
2, 0
2, 1
2, 2
2, 3
3, 0
3, 1
3, 2
3, 3
BUILD SUCCESSFUL (total time: 0 seconds)
• June 23rd, 2010, 04:36 PM
javapenguin
Re: Crazy array
I thought I tried that but it printed

o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o

oooo
oooo
oooo
oooo
• June 23rd, 2010, 05:46 PM
copeg
Re: Crazy array
Quote:

Originally Posted by javapenguin
I thought I tried that but it printed

o
o
o
o
o
o
o
o
o
o
o
o
o
o
o
o

oooo
oooo
oooo
oooo

If you want that particular output with multiple prints on the same line, make sure you utilize System.println and System.print appropriately. The first will print the parameter as a whole line, whereas the other will print the parameter only.
• June 23rd, 2010, 07:43 PM
javapenguin
Re: Crazy array
Also, how would you add scroll bars to a jframe itself, not just have one in it?
• June 23rd, 2010, 07:50 PM
Norm
Re: Crazy array
JScrollBar is a component that can be added to a JFrame. Then it'd be up to you to determine what is viewable on the JFrame as determined by the JScrollBar settings.
• June 23rd, 2010, 09:02 PM
javapenguin
Re: Crazy array
I meant so I could scroll the JFrame down if I had more buttons than would fit.
• June 23rd, 2010, 09:43 PM
Norm
Re: Crazy array
That would be what is viewable means. To change what is shown by moving the viewable area up, down, left or right.
• June 24th, 2010, 08:21 AM
Faz
Re: Crazy array
JP I know we're here to help but have you ever thought of actually trying these things out for yourself to find out? Even if it doesn't work you learn something and then you try something new you need to learn how to figure out these problems yourself by playing around with code a bit not by asking questions on the forum everytime something happens that you don't expect. Also use Google the amount of time(my own and other peoples) I've saved just by a simple Google search is immeasureable.

Example JScrollBar First 3 hits on Google
JScrollBar (Java 2 Platform SE v1.4.2)
A quick demonstration of JScrollBar both vertical and horizontal : ScrollBarSwing JFCJava
How to use JScrollBar class? - Software Development

Why not read them and you know pretty much everything you might need to know about it. And if you didn't know about that class just search "JFrame scrollbar" and your golden. You need to learn to figure these things out for yourself or you won't get anywhere.
• June 25th, 2010, 03:42 AM
Charlie
Re: Crazy array
Print all the elements out of one dimension and then swap line when you hit the end, if you loop over it I'd say using System.print(variable) using the array element and if it has no more elements you use the value "\n" for the print.

Right now you're both setting values to the array and printing it in the printArray method. I'm still not sure if you want to manage the bounds of the array and always use the element values as 'O' or if you want to have it as 4*4 at all times and just manage element values tho. If you only want to print whats in the array like:

Array [0] [0] Array [0] [1] Array [0] [2] Array [0] [3]
Array [1] [0] Array [1] [1] .... etc

you can use this I think.

Code :

``` for (i = 0; i < arrayOfOs.length; i++) { for (j = 0; j < arrayOfOs[i].length; j++) { System.out.print("arrayOfOs[i][j]"); } System.out.print("\n"); }```

Now I have to go be social. Midsummers eve is sort of a big deal in sweden. Hope it helps :)

Edit: 3*3 -> 4*4
• June 25th, 2010, 02:53 PM
javapenguin
Re: Crazy array
Ok, so far I can add one, but it's too small and doesn't do what I wanted. How do you get it to be the same size as the JPanel?
• June 25th, 2010, 03:18 PM
javapenguin
Re: Crazy array
I can't get them to work. I've tried all day. Can't find anything.

I can't extend JPanel as I need to extend JFrame for other things.
• June 26th, 2010, 03:02 AM
Charlie
Re: Crazy array
Extending classes can only be done with one class per class. It only means "this class will inherit all the methods from the class it extends". What you usually want when you extend a JFrame is to have the class itself be the contentpane for the things happening. What you want when you extend JPanel is usually to draw something within the panel itself. Just make another class called SuperPanel or something which in turn extends JPanel and then create a SuperPanel instead of the JPanel.

Easiest way to graphically output the array would probably be to create 4*4 JLabels, put them in a gridlayout inside a panel with the layout GridLayout(4,4) and then put the text ont he labels to element[0][1], [0,2] etc.

Code :

```  public class SuperPanel extends JPanel{   @Override public class paint(graphics g){ //Things you want to draw here. }   }```

Something like that, dont have an IDE on this comp so youll want to check that out before using it, and ofc add things to the paint method. A good tip for this sort of thing would be to initially never extend anything, most things are doable without extending and its good practice to be able to code without extending, since itll make your life so much easier when you actually do start extending.

Hope this helps, gonna go be hung over again.