Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Crazy array

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

    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?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

  3. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 14th, 2010)

  4. #3
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

    Default 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
    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?
    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.

  5. The Following User Says Thank You to Faz For This Useful Post:

    javapenguin (June 16th, 2010)

  6. #4
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Crazy array

    Quote Originally Posted by Faz View Post
    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
    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?
    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.

  7. #5
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

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

    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

  8. #6
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

    Default 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

  9. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  10. #8
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

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

     
     
    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)

  11. The Following User Says Thank You to Charlie For This Useful Post:

    javapenguin (June 23rd, 2010)

  12. #9
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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

    instead of
    oooo
    oooo
    oooo
    oooo

  13. #10
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,318
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: Crazy array

    Quote Originally Posted by javapenguin View Post
    I thought I tried that but it printed

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

    instead of
    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.

  14. The Following User Says Thank You to copeg For This Useful Post:

    javapenguin (June 23rd, 2010)

  15. #11
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Crazy array

    Also, how would you add scroll bars to a jframe itself, not just have one in it?

  16. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

  17. The Following User Says Thank You to Norm For This Useful Post:

    javapenguin (June 23rd, 2010)

  18. #13
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Crazy array

    I meant so I could scroll the JFrame down if I had more buttons than would fit.

  19. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

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

  20. #15
    Member Faz's Avatar
    Join Date
    Mar 2010
    Posts
    97
    Thanks
    5
    Thanked 14 Times in 14 Posts

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

  21. The Following User Says Thank You to Faz For This Useful Post:

    javapenguin (June 24th, 2010)

  22. #16
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

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

     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

  23. The Following User Says Thank You to Charlie For This Useful Post:

    javapenguin (June 25th, 2010)

  24. #17
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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?

  25. #18
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

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

  26. #19
    Member Charlie's Avatar
    Join Date
    Jun 2010
    Location
    Sweden
    Posts
    41
    Thanks
    1
    Thanked 5 Times in 5 Posts

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

     
    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.

Similar Threads

  1. [SOLVED] Create new Array from old Array
    By satory in forum Collections and Generics
    Replies: 1
    Last Post: February 24th, 2010, 12:44 PM
  2. Object array to int array?
    By rsala004 in forum Collections and Generics
    Replies: 1
    Last Post: October 30th, 2009, 04:09 AM
  3. Storing an array into an array
    By vluong in forum Collections and Generics
    Replies: 4
    Last Post: September 22nd, 2009, 02:14 PM
  4. [SOLVED] NullPointerException in Poker's program
    By dean_martin in forum Exceptions
    Replies: 10
    Last Post: April 21st, 2009, 06:40 AM