Results 1 to 4 of 4
  1. #1
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Trouble with an array/loop

    My code is:

    public class Homework12 {

    int array1[];
    int arraySize;
    int output;
    int arrayValue;
    int loop;

    public void setRange()
    {
    arraySize = 15;
    }

    public void createArray()
    {
    array1 = new int[arraySize];
    }

    public void fillArray()
    {
    arrayValue = 0;
    loop = 1;
    do
    {
    array1[arrayValue] = loop;
    System.out.println(array1[loop]);
    arrayValue++;
    loop++;
    }
    while (arrayValue <= (arraySize+1)); //?????
    }

    as you can see, I print out the value of array1[loop], which seems like it should increase by one every time. However, it instead just posts the value of 0 every time. Also, I did a println of array1[arrayValue] in the same place as the current print line and it gradually increased like it was supposed to. Anybody have any idea why?

    Thanks

  2. #2
    StormyWaters is offline Senior Member
    Join Date
    Feb 2009
    Posts
    305
    Rep Power
    6

    Default Re: Trouble with an array/loop

    Look at the index in the array you are filling and the index in the array you are displaying... Are they the same?

  3. #3
    Ronin is offline Senior Member
    Join Date
    Oct 2010
    Posts
    384
    Rep Power
    5

    Default Re: Trouble with an array/loop

    Beaten to it :(

  4. #4
    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Default Re: Trouble with an array/loop

    Ohhh I see what I was doing wrong.

    Thank you

Similar Threads

  1. trouble placing a loop in code
    By james75 in forum New To Java
    Replies: 1
    Last Post: 07-08-2012, 09:20 PM
  2. For loop trouble
    By 2wyked in forum New To Java
    Replies: 7
    Last Post: 03-27-2011, 05:50 PM
  3. Trouble with for loop
    By subiedude101 in forum New To Java
    Replies: 4
    Last Post: 02-13-2011, 09:27 PM
  4. trouble with loop
    By aamster in forum New To Java
    Replies: 10
    Last Post: 10-16-2009, 09:18 PM
  5. [SOLVED] Trouble with this loop!
    By PureAwesomeness in forum New To Java
    Replies: 35
    Last Post: 02-02-2009, 07:04 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •