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: split() method question

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question split() method question

    Hello, i'd like to know what do the following mean in java?

    .split("/")
    .split("\\.")

    Also while iterating, for example:
    [code=Java]
    for(i=0; i<numbers.length;i++)
    [/code=Java]

    when is i=0 and i=1 used? how do i know when to use 0 or 1?

    Thanks for your answers in advance.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,067
    Thanks
    47
    Thanked 2,146 Times in 2,119 Posts

    Default Re: split() method question

    Those are calls to the method: split()

    Read the API doc for the String class to see what the split() method does.
    Java Platform SE 7


    Array indexes always start at 0.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: split() method question

    Quote Originally Posted by hemla View Post
    when is i=0 and i=1 used? how do i know when to use 0 or 1?
    Here is a basket. There are 10 eggs in the basket.
    The computer sees egg numbers 0, 1, 2, 3, 4, 5, 6, 7, 8, 9; you start with zero
    The program is to print out stickers to number the eggs for a party. You want 1, 2, 3, 4, 5, 6, 7, 8, 9, 10; you start with 1

    But as far as your code sample:
    for(i=0;i<numbers.length;i++)
    For the egg example a println on numbers.length would print 10. In the above for loop i would be 0 on the first iteration and 9 on the last iteration that executed the body of the loop, and 10 on the cycle that exits the loop. So in a sense they are 1 through 10 as people count them, but the computer counts them 0 through 9.
    Consider the egg samples:
    //Note i=0 and the use of <
    for(i=0;i<eggsInBasket;i++) {
       //println: This is egg number i+1;
    }
    //vs the following loop using i=1 to do the same:
    //Note i=1 and the use of <=
    for(i=1; i<=eggsInBasket;i++) {
       //println: This is egg number i;
    }
    In the first loop you have to add 1 to i to print 1-10 rather than 0-9.
    In the second loop you print 1-10 without that addition.
    Rather than use i=1 you sometimes see something like:
    for(int i=0; i<eggsInBasket; ) {
       System.out.println("This is egg number: " + ++i);
    }
    ...where the loop is controlled within the body being executed.

  4. The Following User Says Thank You to jps For This Useful Post:

    hemla (April 24th, 2013)

Similar Threads

  1. Java method question
    By Scorks in forum Java Theory & Questions
    Replies: 1
    Last Post: April 12th, 2013, 11:37 AM
  2. calling method question.
    By jasonrest in forum Object Oriented Programming
    Replies: 4
    Last Post: March 26th, 2013, 06:42 PM
  3. Question on a method?
    By cam25 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: June 17th, 2012, 02:53 PM
  4. [SOLVED] Problem with split() method
    By andreas90 in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: January 21st, 2012, 05:15 PM
  5. Thread Hangs at split method.
    By kailasvilaskore in forum Threads
    Replies: 1
    Last Post: November 26th, 2010, 11:13 AM