Results 1 to 8 of 8
  1. #1
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default problem with loop

    Hi!

    Assume that I have 5 variables: a1,...,a5.

    I wonder if it is possible to do a loop like that:
    Java Code:
    for(int i=1; i<=5; i++){
    
    a[i] = "whatever";
    
    }
    I would like to change all my variables "a". Can you help me?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: problem with loop

    I would like to change all my variables "a"
    You have an array named: a that can hold some values (don't know how many because you do not show the size of the array).

    Where are the variables: a1,a2, etc How are those variables related to the array?
    What do you mean by 'change all my variables "a"'?

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: problem with loop

    Quote Originally Posted by pietr0 View Post
    Assume that I have 5 variables
    ...
    Java Code:
    for(int i=1; i<=5; i++){
    
    a[i] = "whatever";
    
    }
    Array indexes start at 0 so your loop should also start at 0. Or you should use
    array[i-1] = ...
    to reference the array item.

    And yes you can change all the values to A exactly how you've written it except changing Whatever to A.

  4. #4
    pietr0 is offline Member
    Join Date
    Dec 2011
    Posts
    6
    Rep Power
    0

    Default Re: problem with loop

    Quote Originally Posted by Norm View Post
    You have an array named: a that can hold some values (don't know how many because you do not show the size of the array).

    Where are the variables: a1,a2, etc How are those variables related to the array?
    What do you mean by 'change all my variables "a"'?
    There is no array.
    I just want to change values of those int variables (named: a1, ..., a5) at the same time.
    I do not want to change their values individually. Perhaps in some place in my program I need to "reset" value of every variable (a5=10 and I need a5=0...).

    I wonder if there is possibility to "reset" their values using a loop instead of write individually a1=0; a2=0; ... ; a5=0 (and in some extreme cases a10000=0;) ??
    I'm just curious...
    Last edited by pietr0; 12-31-2011 at 04:21 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: problem with loop

    I just want to change values of those int variables (named: a1, ..., a5) at the same time.
    This will change all those variables in one statement:
    a0=a1=a2=a3=a4=a5=0;

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: problem with loop

    Quote Originally Posted by pietr0 View Post
    There is no array.
    I just want to change values of those int variables (named: a1, ..., a5) at the same time.
    I do not want to change their values individually. Perhaps in some place in my program I need to "reset" value of every variable (a5=10 and I need a5=0...).

    I wonder if there is possibility to "reset" their values using a loop instead of write individually a1=0; a2=0; ... ; a5=0 (and in some extreme case a10000=0;) ??
    I'm just curious...
    It seems as if you are afraid of arrays; an array 'a' is like a block of houses with house numbers 0, 1, 2, 3 etc. so a[0] is a house, a[1] is a house, etc. and each house serves as a single variable. You can set each variable to, say, 42, like this:

    Java Code:
    for (int i= 0; i < a.length; i++)
       a[i]= 42;
    You can't use a loop with individual variables a0, a1, a2 etc. in Java

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: problem with loop

    Add the items to an array like this:

    int[] a = new int[] { a1, a2, a3, a4, a5 };

    So you'll be able to loop through the values with
    a[i] = ...
    where 0 <= i <= 4
    Last edited by ozzyman; 12-31-2011 at 05:07 PM.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: problem with loop

    @ozzyman
    ??? What is your code supposed to do?

Similar Threads

  1. Replies: 2
    Last Post: 11-07-2011, 02:25 AM
  2. simple line problem / for loop problem
    By helpisontheway in forum New To Java
    Replies: 1
    Last Post: 11-17-2009, 06:12 AM
  3. Problem with loop
    By Tykk in forum New To Java
    Replies: 7
    Last Post: 10-04-2009, 10:26 PM
  4. Some while loop problem need help
    By shaggyoo7 in forum New To Java
    Replies: 4
    Last Post: 01-14-2009, 07:16 PM
  5. Loop Problem
    By jralexander in forum New To Java
    Replies: 4
    Last Post: 12-02-2008, 07:08 AM

Posting Permissions

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