Results 1 to 7 of 7
  1. #1
    End
    End is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default Printing a range of elements from an Array

    Sorry for the vague title, could not think of a better fitting one.

    I have an Array and I would like to print a range of index's from this Array, so for example:

    My Array could contain the letter A in index [0], the letter B in index [1] and so on. Is there anyway for me to print the letters A B C D contained in an Array of the whole alphabet, onto one line. So in other words print the first 4 letter of the alphabet.

    System.out.println(alphabet[0-3]);

    This of course only minuses 3 from 0.

    Thanks for reading :), any tips or pointer's in the right direction appreciated.
    Last edited by End; 04-17-2009 at 07:34 PM.

  2. #2
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    You could use a for loop
    Java Code:
    for(int i = 0; i <=3; i++) {
    	System.out.println( myArray[i] );
    }
    Just change the 3 to whatever the limit you want to iterate to is. You can also change i to start out at 1 if you want to print the array [1-3].

    Mr.Beans

  3. #3
    End
    End is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Hey, thanks for the fast reply :). I dont know if I am doing it wrong but this prints:
    A
    B
    C
    D

    I would like to try and print:
    A B C D

    Sorry for not explaining this clearly :o

  4. #4
    emceenugget is offline Senior Member
    Join Date
    Sep 2008
    Posts
    564
    Rep Power
    7

    Default

    instead of using println, just use print.

  5. #5
    End
    End is offline Member
    Join Date
    Apr 2009
    Posts
    3
    Rep Power
    0

    Default

    Oh wow! I can not believe it was that simple. Thanks very much :D.

  6. #6
    Tshegofatsom is offline Member
    Join Date
    Apr 2009
    Location
    Pretoria, Gauteng, South Africa
    Posts
    43
    Rep Power
    0

    Default

    I dont think you should have given the source code. There will be no learning if we provide the source code.
    Tshegofatso Manakana
    a.k.a Untouchable

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Tshegofatsom... If your refereing to the snippet of code (it's not a whole program) that Mr.Bean posted, I don't think there to much problem with that little piece of code. I do agree with you that complete solutions/programs should not be posted, because there's is no learning process involved (cut & paste & forget).

    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

Similar Threads

  1. Replies: 8
    Last Post: 04-19-2009, 06:50 PM
  2. [SOLVED] Expected Range: Red Green Blue
    By AndrewM16921 in forum New To Java
    Replies: 1
    Last Post: 04-03-2009, 08:59 PM
  3. Replies: 3
    Last Post: 08-24-2008, 02:51 PM
  4. Replies: 1
    Last Post: 08-07-2007, 07:13 AM
  5. Can I set a range in case statement?
    By christina in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:41 PM

Posting Permissions

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