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

Thread: Simplify 2 for loops into one

  1. #1
    Junior Member
    Join Date
    Feb 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Simplify 2 for loops into one

    Hi all,

    I am new to this forum and in need of help for my course of study.

    Below is my java code:

    for(int i=1; i<barcode.length(); i+=2)
    {
    char x = barcode.charAt(i);
    String y = Character.toString(x);
    int z = Integer.parseInt(y);
    evendigit = evendigit + z;
    }


    for(i=0; i<barcode.length(); i+=2)
    {
    char x = barcode.charAt(i);
    String y = Character.toString(x);
    z = Integer.parseInt(y);
    odddigit = odddigit + z;

    }

    The difference is i=1 and i=0;
    I have tried using while loops but failed.
    Please provide some hints.

    Thank you all!

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Simplify 2 for loops into one

    If you use a for loop and look at each of the characters in turn with the loop's index, the value of the index can be used with an if statement to detect the position of the character. The index's value will be either even or odd. Use the modulus operator to check the index's value:
    theIndex % 2 will return 0 or 1 depending on if the index is even or odd

    Please wrap all posted code in code tags.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2018
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Simplify 2 for loops into one

    Quote Originally Posted by Norm View Post
    If you use a for loop and look at each of the characters in turn with the loop's index, the value of the index can be used with an if statement to detect the position of the character. The index's value will be either even or odd. Use the modulus operator to check the index's value:
    theIndex % 2 will return 0 or 1 depending on if the index is even or odd

    Please wrap all posted code in code tags.
    Thanks for your reply.
    I will try it out!

    Sorry its my first time here, will take note in the future!

    Thank you!!!

Similar Threads

  1. Replies: 6
    Last Post: March 12th, 2014, 12:43 PM
  2. How to Simplify my code ?
    By aboyahia1435 in forum Collections and Generics
    Replies: 5
    Last Post: December 12th, 2013, 06:00 AM
  3. Replies: 5
    Last Post: June 17th, 2013, 06:54 PM
  4. For Loops
    By Shyamz1 in forum Loops & Control Statements
    Replies: 3
    Last Post: September 27th, 2011, 11:54 AM
  5. help with loops... :(
    By Macgrubber in forum Loops & Control Statements
    Replies: 2
    Last Post: November 2nd, 2010, 12:38 PM

Tags for this Thread