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

Thread: I need a simple loop code

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default I need a simple loop code

    Hello, I have a very low level knowledge of coding. I have a code that works inside of Adobe Live Cycle (for a PDF). The formula pre-populates feils based on the selection in the first feild. What I have created below works perfectly, but I do not want to write a string of code for every number from 1-200. I know there is a way to fix this, I just do not know how to code it. Can anyone help and give me the exact code I would need to type to create this and how I place it.

    if (this.rawValue == 0)
    {
    this.parent.Billing_State.selectedIndex = 0;
    this.parent.Rep_Code.selectedIndex = 0;
    this.parent.SD_Account.selectedIndex = 0;
    }
    if (this.rawValue == 1)
    {
    this.parent.Billing_State.selectedIndex = 1;
    this.parent.Rep_Code.selectedIndex = 1;
    this.parent.SD_Account.selectedIndex = 1;
    }
    if (this.rawValue == 2)
    {
    this.parent.Billing_State.selectedIndex = 2;
    this.parent.Rep_Code.selectedIndex = 2;
    this.parent.SD_Account.selectedIndex = 2;
    }
    if (this.rawValue == 3)
    {
    this.parent.Billing_State.selectedIndex = 3;
    this.parent.Rep_Code.selectedIndex = 3;
    this.parent.SD_Account.selectedIndex = 3;
    }


  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: I need a simple loop code

    Look at using a for loop. See the tutorial for details:
    The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)

    Also there are lots of sample codes here on the forum that use for loops.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    Thank you for the link. I have already been there and read it. I have very little experince, so I do not know how to apply what is at the link to my specific scenario. If anyone can show me more specifically how to apply the for loop to my exact situation, it would be much appreciated.

  4. #4
    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: I need a simple loop code

    How do you want to use a loop with the code posted in post#1?

    What is the relationship between the value of the variable: rawValue and the value assigned to the 3 variables inside all the if statements?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    I have a list with 200 Names, their state, a code assigned to them, and an account number. The integers are assigned to the correlating information on those lists via Adobe.

    The list would start like this:

    ABC Co – CA – AAA – 12345
    EFG Co – AZ – BBB – 54321

    Adobe assigns a value to each item in the list based on its order:
    ABC Co(0) – CA(0) – AAA(0) – 12345(0)
    EFG Co(1) – AZ(1) – BBB(1) – 54321(1)

    Adobe assigns an integer to each number starting at 0, thiss integer is used to reference the information, and pull it up.

    In the list above ABC Co is rawValue
    CA is Billing_State
    AAA is Rep_Code
    12345 is SD_Account.

    Even though all 4 lists are separate, I will be referencing the same number accross each list, every time (like I have started to type in my code).

    Example: I will always reference the number 1 across each list, I will never reference 1 from the 1st list 4, from the second, 3 from the 3rd and 11 from the fourth.

    Would something like what I have below work (I’m sure there are many errors in what I just typed)

    public static void main(String[] args){
    for(int i=1; i<200; i++){
    if (this.rawValue == i)
    {
    this.parent.Billing_State.selectedIndex = i;
    this.parent.Rep_Code.selectedIndex = i;
    this.parent.SD_Account.selectedIndex = i;
    }}

  6. #6
    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: I need a simple loop code

    Would something like what I have below work
    What happens when you compile and execute it?

    What is the relationship between the value of the variable: rawValue and the value assigned to the 3 variables inside all the if statements?
    Why the need for a loop? Why not assign directly?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    Thank you for the help!

    The relationship between the 4 is that they all share the same integer value that has been assigned to them. They appear in 4 different places on a form, but they are all related and belonging to 1 company. There are 200 companies that have 1 relationship with the 3 other variables.

    I'm not exactly sure that I need a loop. What I really trying to figure out is if there is a way of writing the code below without having to wright the same code over and over 200 times, when all I am doing is increasing the number by one.

    if (this.rawValue == 0)
    {
    this.parent.Billing_State.selectedIndex = 0;
    this.parent.Rep_Code.selectedIndex = 0;
    this.parent.SD_Account.selectedIndex = 0;
    }
    if (this.rawValue == 1)
    {
    this.parent.Billing_State.selectedIndex = 1;
    this.parent.Rep_Code.selectedIndex = 1;
    this.parent.SD_Account.selectedIndex = 1;
    }
    if (this.rawValue == 2)
    {
    this.parent.Billing_State.selectedIndex = 2;
    this.parent.Rep_Code.selectedIndex = 2;
    this.parent.SD_Account.selectedIndex = 2;
    }
    if (this.rawValue == 3)
    {
    this.parent.Billing_State.selectedIndex = 3;
    this.parent.Rep_Code.selectedIndex = 3;
    this.parent.SD_Account.selectedIndex = 3;
    }

  8. #8
    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: I need a simple loop code

    they all share the same integer value
    That should make the light come on. If you ALWAYS want to assign the value of the rawValue to the three fields, why all the tests? One if test to check that the value is in the desired range and then 3 assignment statements. Done.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Sep 2012
    Posts
    128
    Thanks
    1
    Thanked 14 Times in 14 Posts

    Default Re: I need a simple loop code

    if the first assigned value is 0, why is your loop starting with 1?

  10. #10
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    Norm,
    Thank you again for your help. Could you show me how that would look?

    Starstreak,
    The loop I made up above is something I basically guessed at when putting together. I should have put i = 0, but I'm sure there are 10 other isues that could be pounted out with that code. On a scale of 1-10 for Java experience, I am a 1.5.

  11. #11
    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: I need a simple loop code

    What part are you having trouble with?
    The if test to check if the value of rawValue is in range. What are the valid values?
    The 3 assignment statements

    pseudo code:
    test if in range
    if true, then assign values to the three variables
    If you don't understand my answer, don't ignore it, ask a question.

  12. #12
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    Norm,

    Thanks again for your help. Can you show me what this would actually look like in code. What I am reading right now is like picking up a book in French. Any help is appreciated.

  13. #13
    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: I need a simple loop code

    What statements are giving you the problem?
    The if statement? You are using them in the currently posted code.
    See: The if-then and if-then-else Statements (The Java™ Tutorials > Learning the Java Language > Language Basics)
    The assignment statements? Again there are assignment statements in the code.
    If you don't understand my answer, don't ignore it, ask a question.

  14. #14
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    If I created something like this:

    if (this.rawValue == i)
    {
    this.parent.Billing_State.selectedIndex = i;
    this.parent.Rep_Code.selectedIndex = i;
    this.parent.SD_Account.selectedIndex = i;
    }


    How do I get the whole numbers 1-200 assigned to i? Maybe I am trying to attack this problem wrong?

    If I use the if statment, doen't that lead me back to where I started, having to write the same code 200 times?

  15. #15
    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: I need a simple loop code

    Why not do this:
    this.parent.Billing_State.selectedIndex = rawValue;
    If you don't understand my answer, don't ignore it, ask a question.

  16. #16
    Junior Member
    Join Date
    Mar 2013
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: I need a simple loop code

    Thak you, works great!

Similar Threads

  1. Simple Java Code Help--- temperature conversion using while loop
    By Idiot_willing_to_learn in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 26th, 2013, 11:33 AM
  2. how do i loop this simple java code?
    By legend101z in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 27th, 2013, 06:46 PM
  3. hello! can i please get some help with a simple loop problem?
    By zerocool18 in forum Loops & Control Statements
    Replies: 6
    Last Post: October 13th, 2012, 10:31 AM
  4. Simple Do Loop Problem
    By inflames098 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 10th, 2012, 03:43 PM
  5. Simple Nested Do/While Loop
    By Farmer in forum Loops & Control Statements
    Replies: 2
    Last Post: July 25th, 2011, 08:31 AM