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

Thread: HELP ME HOW TO DECREMENT THIS PLS

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default HELP ME HOW TO DECREMENT THIS PLS

    int [ ] arr = new int[ 4 ]
    for(int i = 0; i < arr.length; i++)
    arr[ i ] = i + 1
    System.out.println(arr[ i ]++)


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    What is your question? What is the problem? What does this code do? What is it supposed to do? Please see the link in my signature on asking questions the smart way.

    You decrement exactly how you increment, only with subtraction instead of addition.

    Also, I recommend you surround the body of your for loop with curly brackets { }, otherwise you're only executing the single line following the loop.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    i did it on purpose..when i use -- it says outofbounds

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

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    it says outofbounds
    That JVM is fussy about you're using indexes that go past the end of an array.
    Check your code to be sure you do not allow that to happen.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    Quote Originally Posted by jeremanalaotao View Post
    i did it on purpose..when i use -- it says outofbounds
    Okay, and what did you expect to happen?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    int [ ] arr = new int[ 10 ];

    for(int i =8; i < arr.length; i--){
    arr[ i ] = i - 1;
    System.out.println(arr[ i ]);


    this is my code for decrement..but it says "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1"

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    Quote Originally Posted by jeremanalaotao View Post
    int [ ] arr = new int[ 10 ];

    for(int i =8; i < arr.length; i--){
    arr[ i ] = i - 1;
    System.out.println(arr[ i ]);


    this is my code for decrement..but it says "Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1"
    When do you expect that loop to exit?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    i want to exit that at 0 hehe

  9. #9
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    Quote Originally Posted by jeremanalaotao View Post
    i want to exit that at 0 hehe
    So now do you have it figured out?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  10. #10
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    not yet...zzzzzzz

  11. #11
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: HELP ME HOW TO DECREMENT THIS PLS

    Well, look at your exit condition- it's the second part of the for loop declaration (i < arr.length). The for loop exits when that evaluates to false. But if you're starting at length-1 and only decrementing, that will never be false.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  12. #12
    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: HELP ME HOW TO DECREMENT THIS PLS

    You must read the syntax of the for loop. Kevin has provided you with the best answer. He told you your condition.
    So for loop's syntax is actually
    Syntax:
    for(initialization;condition;iteration){
    //body of loop;
    }
    And you have successfully done your initialization and iteration (decrement). You have problem in your condition.
    You said you want for loop to run till 0.
    You are starting from 8. Your array size is 10. You are actually starting from 8 and you are limiting your loop to go till size of array (10).
    First time your loop starts, value is 8 that is less than 10, true. Works fine. You decrement one and value becomes 7.... and 6,5,4,3,2,1,0,-1 and so on....
    You will never have array index as -1. And this is the problem. Index out of bounds.
    Now think and try to solve.
    Luck

Similar Threads

  1. [SOLVED] Help with prefix and postfix(increment&decrement)
    By Lokesh in forum Object Oriented Programming
    Replies: 1
    Last Post: February 12th, 2011, 10:20 AM