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.

Page 1 of 2 12 LastLast
Results 1 to 25 of 47

Thread: Array and loops.

  1. #1
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Array and loops.

    Hello guys,
    So I decided to experiment with what I know.
    Start out with a small program then build on it till I used everything I know, with out making any mess.
    I came up with an idea of a program that will tell me who's birthday it is and how many days are left for the other peoples birthdays (I used 7 people).

    class BirthdaySurprise {
        public static void main(String[] args){
            // here are all the people
            byte TSE = 122;
            short LA = 184;
            short ZE = 195;
            short AB = 199;
            int DE = 234;
            int HA = 349;
            int AL = 361;
            //That's the last one. The numbers are the number of the day that 
            //each person's birthday comes on. So if your birthday comes on Jan 1 
            //your variable will be equal to 1, and if on Dec31 it will be equal to 365.   
            for( byte aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;
                 byte[] aryOne = new byte[aryOneAdder];
                 System.out.println(aryOne[0]);
            }
     
        }
    }
    Output:
    run:
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0
    0( 121 zero's in all )

    What went wrong in that loop?

    Oh and the aryOne array is made in the for loop, that means I wont be able to use it out of the loop right?


  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: Array and loops.

    byte[] aryOne = new byte[aryOneAdder]; // Create a new byte array with aryOneAdder elements

    Your loop creates a new array every time it loops. That replaces the one that was created on the last loop

    store all the numbers in the aryOne array
    To do that create the array of the size you want, OUTSIDE the loop and then in the loop assign each value to an element of the array:
    aryOne[aryOneAddr] = aryOneAddr; // assign value to array element: value equals the index

    ( 121 zero's in all
    The default value for an int array element is 0

  3. #3
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Like this?
    byte aryOneAdder;
            for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;          
            }
            byte[] aryOne = new byte[123];
            aryOne[aryOneAdder] = aryOneAdder;
            System.out.println(aryOne[0]);

    That gave me an out of bounds error message.
    Last edited by Melawe; July 29th, 2010 at 10:13 PM.

  4. #4
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Any clue to what woke up indexOutOfBounds?

  5. #5
    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: Array and loops.

    There is important info in the error message, please copy and paste here the full text of the message.

    It looks like you create the array AFTER you try to use it.
    Create the array BEFORE you use it.

  6. #6
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    After turning of the pc for a few hours and trying again oddly the output has changed.
    Output:

    run:
    0
    BUILD SUCCESSFUL (total time: 1 second)

    Even before changing the place of the array.

  7. #7
    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: Array and loops.

    Magic. Or you've got a ghost changing your program.

  8. #8
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    My guess: last time a virus messed up Netbeans I uninstalled it and re-downloaded it, but looks like some crazy files where left behind somewhere. :/ I'm planing on uninstalling all programs and downloading them again, might be the registry playing games on me but lets not hope its that.

    Anyway it still prints out 0, and not 1-121, you know why?

    P.S. If you know any program that can fix the registry leave a link plz.

  9. #9
    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: Array and loops.

    Try playing computer with your program to see how how the values of the variables change.
    Go thru the code line by line and record what variables are set to what values.

    At the end what is the value in aryOne[0] ?
    Look at where you create the aryOne array. When its created all its element values are 0
    Last edited by Norm; July 30th, 2010 at 04:24 PM.

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

    Melawe (July 31st, 2010)

  11. #10
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Thanks! That sure is a different way of looking at it!

  12. #11
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Should I find something strange in the output of
    byte aryOneAdder;
            byte[] aryOne = new byte[123];
            for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;   
                 aryOne[0] = aryOne[aryOneAdder];
            }        
     
            System.out.println(aryOne[0]);
    and
    for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;   
                 aryOne[aryOneAdder] = aryOne[0];
            }        
     
            System.out.println(aryOne[0]);]
    and
    byte aryOneAdder;
            byte[] aryOne = new byte[123];
            for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;          
            }        
            aryOne[aryOneAdder] = aryOneAdder;
            System.out.println(aryOne[0]);
    all being

    run:
    0
    BUILD SUCCESSFUL (total time: 1 second).

    I have under-looked something or Kaspersky needs reinstalling.

  13. #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: Array and loops.

    To see what is in the array after your code executes use this:

    System.out.println(Arrays.toString(aryOne)); // Show array's contents

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

    Melawe (July 31st, 2010)

  15. #13
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    class BirthdaySurprise {
        public static void main(String[] args){
            // here are all the people
            byte TSE = 122;
            short LA = 184;
            short ZE = 195;
            short AB = 199;
            int DE = 234;
            int HA = 349;
            int AL = 361;
            //That's the last one. The numbers are the number of the day that 
            //each person's birthday comes on. So if your birthday comes on Jan 1 
            //your variable will be equal to 1, and if on Dec31 it will be equal to 365.
            byte aryOneAdder;
            byte[] aryOne = new byte[123];
            for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                 aryOneAdder++;   
                 aryOne[0] = aryOne[aryOneAdder];             
            }      
            System.out.println(Array.toString(aryOne)); // Show array's contents
        }
    }

    output:
    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
    at BirthdaySurprise.main(BirthdaySurprise.java:21)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second)


    Output with the print line statement in the loop:

    run:
    Exception in thread "main" java.lang.RuntimeException: Uncompilable source code
    at BirthdaySurprise.main(BirthdaySurprise.java:21)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 2 seconds)

  16. #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: Array and loops.

    What source line is at line 21?
    What import statements do you have in your program?
    You need to import the package that the Arrays class is in?

  17. #15
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    What is the import statement should I use?
    Last edited by Melawe; August 1st, 2010 at 11:00 AM.

  18. #16
    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: Array and loops.

    Quote Originally Posted by Melawe View Post
    What is the import statement I should use?
    I assume you wish to use the Arrays (Java Platform SE 6) class and not Array class...see the package name in the link

  19. #17
    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: Array and loops.

    import java.util.Arrays;

  20. #18
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    import java.util.Arrays;
    class BirthdaySurprise {
        public static void main(String[] args){
            // here are all the people
            byte TSE = 122;
            short LA = 184;
            short ZE = 195;
            short AB = 199;
            int DE = 234;
            int HA = 349;
            int AL = 361;
            //That's the last one. The numbers are the number of the day that 
            //each person's birthday comes on. So if your birthday comes on Jan 1 
            //your variable will be equal to 1, and if on Dec31 it will be equal to 365.
            byte aryOneAdder;
            byte[] aryOne = new byte[123];
            for(aryOneAdder = 1; aryOneAdder <121;){
                //Now what I want to happen here is the for loop increments aryOneAdder
                //till it's equal too 121. And store all the numbers in the aryOne array
                aryOneAdder++;
                aryOne[aryOneAdder] = aryOne[1];
            }      
            System.out.println(Arrays.toString(aryOne)); // line 23
        }
    }

    Output
    [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, ] all zeros :/

    Sorry for my late late reply.

  21. #19
    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: Array and loops.

    aryOne[aryOneAdder] = aryOne[1];
    What is the contents of: aryOne[1] ?
    You are copying it into all of the elements of the array?
    If it contains 0 then your program is working as I'd expect it to.

  22. #20
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    All elements of aryOne are equal to 0.

  23. #21
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Note: this is another program I use to experiment something I am not sure will work, if it works I then apply it to the main program.

    class Paster {
        public static void main(String[] args){
    byte aryOneAdder;
    for(aryOneAdder = 0; aryOneAdder <121;){
    if (aryOneAdder <121){
                aryOneAdder++;
            }
    System.out.println(aryOneAdder);
    }
     
        }
    }

    I tried this and got this output:
    run:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15 to 121.
    Last edited by Melawe; August 4th, 2010 at 12:30 PM.

  24. #22
    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: Array and loops.

    Ok. Is your problem solved now?

  25. #23
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    Not reallyx cause the output of the BirthdaySurprise program is still 0.
    Last edited by Melawe; August 4th, 2010 at 12:07 PM.

  26. #24
    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: Array and loops.

    ??? The last post showed that the output was the numbers from 1 to 121

  27. #25
    Member
    Join Date
    Mar 2010
    Posts
    271
    Thanks
    21
    Thanked 7 Times in 7 Posts

    Default Re: Array and loops.

    That was the output for the Paster program, which I was using to experiment with. But the Birthday Surprise program still has 0 as the output.

Page 1 of 2 12 LastLast

Similar Threads

  1. Java Loops help please?
    By Sarah-Perkin in forum Loops & Control Statements
    Replies: 2
    Last Post: December 6th, 2009, 02:52 PM
  2. Program with for loops help
    By ixjaybeexi in forum Loops & Control Statements
    Replies: 23
    Last Post: October 8th, 2009, 10:05 AM
  3. need help with loops plz
    By Kilowog in forum Loops & Control Statements
    Replies: 4
    Last Post: September 28th, 2009, 08:11 AM
  4. Homework - using 'IF' for 'For Loops'
    By Enzo in forum Loops & Control Statements
    Replies: 5
    Last Post: September 16th, 2009, 10:52 AM
  5. Replies: 11
    Last Post: April 29th, 2009, 03:12 AM