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 2 of 2 FirstFirst 12
Results 26 to 47 of 47

Thread: Array and loops.

  1. #26
    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 is it supposed to output? Can you explain what the code is supposed to do to generate its output?

    Perhaps you should post the current version of the program with an explanation of what it is supposed to do.

  2. #27
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array and loops.

    Ok, I'm going to try to run you through this as detailed as possible. Because Norm explained your problem back on like page 1.

    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]);
    }

    Ok, we'll walk through the first 10 iterations.
    1. aryOneAdder = 2; aryOne.length = 2; aryOne[0] = 0;
    2. aryOneAdder = 3; aryOne.length = 3; aryOne[0] = 0;
    3. aryOneAdder = 4; aryOne.length = 4; aryOne[0] = 0;
    4. aryOneAdder = 5; aryOne.length = 5; aryOne[0] = 0;
    5. aryOneAdder = 6; aryOne.length = 6; aryOne[0] = 0;
    6. aryOneAdder = 7; aryOne.length = 7; aryOne[0] = 0;
    7. aryOneAdder = 8; aryOne.length = 8; aryOne[0] = 0;
    8. aryOneAdder = 9; aryOne.length = 9; aryOne[0] = 0;
    9. aryOneAdder = 10; aryOne.length = 10; aryOne[0] = 0;
    10. aryOneAdder = 11; aryOne.length = 11; aryOne[0] = 0;

    Problems:
    1. Notice how aryOne.length is changing as we go through the array, this shoudnt happen. But why is it happening? Because you construct byte[] aryOne = new byte[aryOneAdder]; INSIDE the loop that is iterating it. Which means that every time the loop iterates, it recreates aryOne with default values of 0. Create the array OUTSIDE of the loop, and problem solved.

    2. You never reference array indexes 0 or 1. This is because you start your referencing at 1, and before you reference anything you increase it by 1. However, you are printing out array index 0, which means that it will ALWAYS print 0.

    Here is my suggested fix:
    //Construct Array Outside of Loop
    byte[] aryOne = new byte[120];
    //Start the loop at 1 AND put your incremention inside the FOR parameters so it will increment at the end
    for( byte aryOneAdder = 0; aryOneAdder <120;aryOneAdder++)
    {
                 //This will store each index with their index value. 
                 aryOne[aryOneAdder] = aryOneAdder
    //Print out the value from the position we just inserted into
                 System.out.println(aryOne[aryOneAdder]);
    }

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

    Default Re: Array and loops.

    The array is created out of the for loop. Here is the current version of the code.

    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 = 0; aryOneAdder <122;){
                    if (aryOneAdder <121){
                aryOneAdder++;
                }
                aryOne[aryOneAdder] = aryOne[1];
             }
                    System.out.println(Arrays.toString(aryOne));
        }
    }

    Since yesterday I've been unable to get any output when I run it, it just shows the progress bar below the output pane in NetBeans and doesn't seem to move at all.

  4. #29
    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.

          for(aryOneAdder = 0; aryOneAdder < 122;){
                    if (aryOneAdder <121){
                         aryOneAdder++;
                     }
                    aryOne[aryOneAdder] = aryOne[1];
                    System.out.println("aryOneAddr=" + aryOneAddr): // Show current value of aryOneAddr
             } // end for()
             System.out.println(Arrays.toString(aryOne));

    Add: System.out.println("aryOneAddr=" + aryOneAddr):
    where its shown above and run the code.
    What happens (or doesn't happen) when aryOneAddr is equal to 121?

    it just shows the progress bar
    That means the program is in a loop.

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

    Default Re: Array and loops.

    Printed out aryOneAddr=121 over 121 times.

  6. #31
    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.

    Is that what you would expect from the code?

    I would think it would print out forever or until the buffer got full or you stopped the program.

    What code in the loop will stop the loop from going forever?

  7. #32
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array and loops.

    That would be because of this statement:
    if (aryOneAdder <121){
                aryOneAdder++;
                }

    There are two ways you can construct this loop. The way you currently have it will not reference index 0 and will never exit the loop. Your conditional statement in your loop, aryOneAdder < 122 will always be true because your if statement above will never allow the value to be larger than 121. So you are getting an infinite loop.

    You seem to be constructing your for loop similar to how a while loop is constructed. To each their own, but I prefer to put the incrementation inside the FOR parameters.

    If you want to reference index 0, here are the two ways of constructing this loop:
    for(aryOneAdder = 0; aryOneAdder < 122;aryOneAdder++)
    {
    //Loop Code
    }
    In this one, notice how our statement, aryOneAdder++ is inside the FOR parameters. This is perfectly legal and the intended design of the FOR loop. The statement aryOneAdder++ will be executed every time the program reaches the end of the loop.

    The second way of creating the loop would be more of a WHILE loop way, but it is more of how you are attempting it:
    for(aryOneAdder = 0; aryOneAdder < 122;)
    {
    //Loop Code
    aryOneAdder++;
    }
    Notice how our aryOneAdder++; statement is at the end of the loop and will execute every time. This will allow you to access array index 0 and still go through the loop.

    Both of these loops do the exact same thing, it is just a different way of coding it.

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

    Talking 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[] aryOne = new byte[122];
            for( byte aryOneAdder = 0; aryOneAdder <=121;aryOneAdder++)
                  {
                 //This will store each index with their index value.
                 aryOne[aryOneAdder] = aryOneAdder;
                //Print out the value from the position we just inserted into             
                }
            System.out.println(Arrays.toString(aryOne));
        }
    }

    Output:
    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121]

    Thanks guys!!! You have been a great deal of help!!


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

    Default Re: Array and loops.

    I'm getting an error message for this loop.

    short[] aryLa = new short[70];
            for( short aryLaAdder = 123; aryLaAdder <=184;aryLaAdder++){
                //This will store each index with their index value.
                 aryLa[aryLaAdder] = aryLaAdder;
                 //Print out the value from the position we just inserted into
                }
            System.out.println("aryLa = " + Arrays.toString(aryLa));

    Error message:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 123
    at BirthdaySurprise.main(BirthdaySurprise.java:25)
    Java Result: 1
    BUILD SUCCESSFUL (total time: 1 second

    Line 25:
      aryLa[aryLaAdder] = aryLaAdder;

  10. #35
    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.

    ArrayIndexOutOfBoundsException: 123
    short[] aryLa = new short[70];
    What is the maximum index value you can have for the array aryLa?
    What is the value of the first index used in your code?

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

    Default Re: Array and loops.

    70 and 123. Is that causing the error? Can't it hold 123 as the first element? It works when I so it this way.
    short[] aryLa = {123,124,125,126,127,128,129,130,131,132,133,134,135,136,
            137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,
            155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,
            173,174,175,176,177,178,179,180,181,182,183};
    Last edited by Melawe; August 6th, 2010 at 02:54 PM.

  12. #37
    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.

    yes, that is the problem

    The contents of the array can be any valid short value. The values of the indexes into the array can only be from 0 to 69.

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

    Question Re: Array and loops.

    Quote Originally Posted by Norm View Post
    The values of the indexes into the array can only be from 0 to 69.
    A bit confused there.

  14. #39
    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.

    short[] aryLa = new short[70];
    With this array definition, the values of the index can be from 0 to 69.
    The element: aryLa[70] does NOT exist.

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

    Melawe (August 7th, 2010)

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

    Default Re: Array and loops.

    I was thinking, my code looks good so far. But it would be much much better if I could make each persons variable equal to their DOB. Is there anyway that would be possible?

  17. #41
    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.

    make each persons variable equal to their DOB.
    What does that mean?
    Is there a variable for each person? What type is the variable? String or int or a new class you're going to create?

  18. #42
    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[] aryTse = new byte[122];
            for( byte aryTseAdder = 0; aryTseAdder <=121;aryTseAdder++){
                //This will store each index with their index value.
                 aryTse[aryTseAdder] = aryTseAdder;
                 //Print out the value from the position we just inserted into
                }
            System.out.println("aryTse = " + Arrays.toString(aryTse));
     
            short[] aryLa = {123,124,125,126,127,128,129,130,131,132,133,134,135,136,
            137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,
            155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,
            173,174,175,176,177,178,179,180,181,182,183};
            System.out.println("aryLa = " + Arrays.toString(aryLa));
     
            short[] aryZe = {185,186,187,188,189,190,191,192,193,194};
            System.out.println("aryZe = " + Arrays.toString(aryZe));
     
            short[] aryAb = {196,197,198};
            System.out.println("aryAb = " + Arrays.toString(aryAb));
     
            int[] aryDe = {200,201,202,203,204,205,206,207,208,209,210,211,213,214,
                  215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
                  232,233};
     
            int[] aryHa = {235,236,237,238,239,240,241,242,243,244,245,246,247,248,
                  249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,
                  266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,
                  283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,
                  300,301,302,303304,305,306,307,308,309,310,311,312,313,314,315,316,
                  317,318,319,320,321,322,323,324};
     
            int[] aryAl = {350,351,352,353,354,355,356,357,358,359,360};
     
            int Today = DE;
     
            switch (Today) {
                case 122 :
                    System.out.println();
                case 184 :
                    System.out.println();
                case 195 :
                    System.out.println();
                case 199 :
                    System.out.println();
                case 234 :
                    System.out.println();
                case 349 :
                    System.out.println();
                case 361 :
                    System.out.println();
                default:
                    System.out.println();
            }
     
        }
    }

    This variables represent the people:
      // 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;

    DOB= Date of Birth.
    Last edited by Melawe; August 12th, 2010 at 12:52 PM.

  19. #43
    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.

    Is your problem solved now?

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

    Default Re: Array and loops.

    It's not a problem actually, I was only wondering if I could do
    int AL = 27/12/93;
    instead of
    int AL = 361;
    //361 being the 361st day of the year

    Did you understand me?
    Last edited by Melawe; August 12th, 2010 at 01:27 PM.

  21. #45
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Array and loops.

    You would have to say
    String AL = "27/12/93";

    I actually just did something with dates today. Made a whole class to allow me to work with mm/dd/yy date format the way I wanted to.

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

    Default Re: Array and loops.

    Would you mind posting the code so I can try to use the class?
    Last edited by Melawe; August 12th, 2010 at 01:39 PM.

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

    Default Re: Array and loops.

    Updated code.

    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[] aryTse = new byte[122];
            for( byte aryTseAdder = 0; aryTseAdder <=121;aryTseAdder++){
                //This will store each index with their index value.
                 aryTse[aryTseAdder] = aryTseAdder;
                 //Print out the value from the position we just inserted into
                }
     
            short[] aryLa = {123,124,125,126,127,128,129,130,131,132,133,134,135,136,
            137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,
            155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,
            173,174,175,176,177,178,179,180,181,182,183};
     
            short[] aryZe = {185,186,187,188,189,190,191,192,193,194};
     
            short[] aryAb = {196,197,198};
     
            int[] aryDe = {200,201,202,203,204,205,206,207,208,209,210,211,213,214,
                  215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,
                  232,233};
     
            int[] aryHa = {235,236,237,238,239,240,241,242,243,244,245,246,247,248,
                  249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,265,
                  266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,
                  283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,
                  300,301,302,303304,305,306,307,308,309,310,311,312,313,314,315,316,
                  317,318,319,320,321,322,323,324};
     
            int[] aryAl = {350,351,352,353,354,355,356,357,358,359,360};
     
            int Today = DE;
     
            switch (Today) {
                case 122 :
                    System.out.println("Happy Birthday Tsehaye!!");
                    break;
                case 184 :
                    System.out.println("Happy Birthday Almaz!!");
                    break;
                case 195 :
                    System.out.println("Happy Birthday Zewdit!!");
                    break;
                case 199 :
                    System.out.println("Happy Birthday Abraham!!");
                    break;
                case 234 :
                    System.out.println("Happy Birthday Desta!!");
                    break;
                case 349 :
                    System.out.println("Happy Birthday Habtam!!");
                    break;
                case 361 :
                    System.out.println("Happy Birthday Alem(one extra ! mark for you :D )!!!");
                    break;
                default:
                    System.out.println("Ohsnap! oh no!!! No birthdays today!!! :O");
            }
     
        }
    }

Page 2 of 2 FirstFirst 12

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