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 15 of 15

Thread: About loop

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default About loop

    Class SetZero{
        int x,y;
        int[][] matrix = new int[200][200];
     
        void setMartixZero(){
            for(x=1 ; x<=200; x++)
                for(y=1; y<=200; y++)
                    matrix[x][y] = 5;                    
        }
     
    }
    There is an error in line 8. Can anyone help me? Thank you.
    Last edited by tommyabc; March 2nd, 2012 at 11:15 AM. Reason: careless mistake for x=0 and y=0 in the loop


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: About loop

    I'm not sure what you are trying to do but
     matrix[x][y]
    is a location in your two-dimensional int array, so you need to assign it an int value. For example
    matrix[x][y] = 13;

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    I want to set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
    I want to use for loop to do so but it needs to work in a method.
    Outside a method, I can't use for loop so i need to write a lot for my purpose.

  4. #4
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: About loop

    Can you explain the problem you are facing now? You have the "setMartixZero()" method which set all the matrix[][] locations equal to 5. You can just call it when you want.

  5. #5
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: About loop

    If you are facing error/exception, paste here the full text so that we could have a look.
    Also, i can assume there must be index out of bound exception. Coz, you are trying to save the value 5 at matrix[200][200] as the total size of your array is 200X200 which means matrix[199][199] will be the last row and column of your array.

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    I made a careless mistake here.
    My code is fine now but I want to ask how i can set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
    by using loop? A loop can only be performed inside a method.

  7. #7
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: About loop

    A loop can only be performed inside a method.
    Can you provide me with the reference material where you've studied that a loop can not perform inside a method???? Loop can be placed anywhere in your code. Place it in main() and don't make a separate method for initializing the array(if only you intended to do this).
    how i can set matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5
    If you want to use matrix[200][200], don't forget to change the size of array to matrix[201][201] or face the exception.

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    Quote Originally Posted by Mr.777 View Post
    Can you provide me with the reference material where you've studied that a loop can not perform inside a method???? Loop can be placed anywhere in your code. Place it in main() and don't make a separate method for initializing the array(if only you intended to do this).

    If you want to use matrix[200][200], don't forget to change the size of array to matrix[201][201] or face the exception.
    Sorry, are there any misunderstanding? I mean that loop can only be placed inside a method. Outside a method, there is an error. So what i should do if I want to declare
    matrix[0][0] = 5, matrix[0][1] = 5, ... , matrix[200][200] = 5.
    Class Matrix{
            int x,y;
            int[][] matrix = new int[201][201];
            for(x=1 ; x<=200; x++) //there will be an error because of outside the method
                for(y=1; y<=200; y++)
                    matrix[x][y] = 5; 
    }
    Thank you.

  9. #9
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    Quote Originally Posted by andreas90 View Post
    Can you explain the problem you are facing now? You have the "setMartixZero()" method which set all the matrix[][] locations equal to 5. You can just call it when you want.
    Do you mean that I can only set all the matrix[][] locations equal to 5 with a method?
    Because I want to do the following thing but it seems so stupid.
     static int[][] matrix = {{5 /* x200 times */ }};
    Thank you.

  10. #10
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: About loop

    You can never write loops inside the class like this. You need to have a main() method to start your program. In the main(), use the loop and do whatever you want to do. Like;
    class A{
       public int x;
       public static void main(String[] args){
       x = 5;
       }
    }

  11. #11
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    Quote Originally Posted by Mr.777 View Post
    You can never write loops inside the class like this. You need to have a main() method to start your program. In the main(), use the loop and do whatever you want to do. Like;
    class A{
       public int x;
       public static void main(String[] args){
       x = 5;
       }
    }
    But I need to initiallise matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5 in a class which does not have main class. May i know that if there are any ways i can do it other than using a method.Thank you

  12. #12
    Member snowguy13's Avatar
    Join Date
    Nov 2011
    Location
    In Hyrule enjoying a chat with Demise and Ganondorf
    Posts
    339
    My Mood
    Happy
    Thanks
    31
    Thanked 48 Times in 42 Posts

    Default Re: About loop

    No, you need to use a method. But it isn't that complicated. When you typed this code:

            for(x=1 ; x<=200; x++)
                for(y=1; y<=200; y++)
                    matrix[x][y] = 5;

    it was very close to perfect. The one thing you must remember is that arrays start at index 0 (not at index 1).
    Use highlight tags to help others help you!

    [highlight=Java]Your prettily formatted code goes here[/highlight]

    Using these tags makes your code formatted, and helps everyone answer your questions more easily!




    Wanna hear something funny?

    Me too.

  13. #13
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default Re: About loop

    Quote Originally Posted by tommyabc View Post
    Do you mean that I can only set all the matrix[][] locations equal to 5 with a method?
    Because I want to do the following thing but it seems so stupid.
     static int[][] matrix = {{5 /* x200 times */ }};
    Thank you.
    The for loops are setting ALL the indexes of your int array equal to 5. This what you want to do as far as i understand.
    Quote Originally Posted by tommyabc View Post
    But I need to initiallise matrix[0][0]=5, matrix[0][1]=5 ... matrix[200][200] = 5 in a class which does not have main class. May i know that if there are any ways i can do it other than using a method.Thank you
    You can put the for loops in a separate method which you can call when you want to initialise "matrix[][]".
    And as Mr.777 and snowguy13 have mentioned be careful with your array indexes (200 indexes are from 0 to 199).

  14. #14
    Junior Member
    Join Date
    Mar 2012
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: About loop

    Quote Originally Posted by snowguy13 View Post
    No, you need to use a method. But it isn't that complicated. When you typed this code:

            for(x=1 ; x<=200; x++)
                for(y=1; y<=200; y++)
                    matrix[x][y] = 5;

    it was very close to perfect. The one thing you must remember is that arrays start at index 0 (not at index 1).
    Then i will use a method to do it. Thank you.

  15. #15
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: About loop

    Or you can use a constructor instead.

Similar Threads

  1. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  2. For loop; Problems with my for loop
    By mingleth in forum Loops & Control Statements
    Replies: 5
    Last Post: November 16th, 2011, 07:24 PM
  3. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 12:05 PM
  4. for loop and while loop problems
    By Pulse_Irl in forum Loops & Control Statements
    Replies: 4
    Last Post: May 3rd, 2010, 02:09 AM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM