Results 1 to 4 of 4

Thread: Array question

  1. #1
    Jbo
    Jbo is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Array question

    Hi All,

    I would like to know what I am doing wrong with the below code. I am trying to create an array and fill it with the letters of the alphabet. My code compiles but does not print out the alphabet.

    class MyArray
    {
    public static void main(String [] args)
    {
    int [] alphabet = new int[26];
    for ( char a = 97; a <= alphabet.length; a++)
    {
    alphabet[a] = a +1;
    System.out.println(alphabet[a]);
    }
    }
    }

    any help would be great.

  2. #2
    kjkrum's Avatar
    kjkrum is offline Senior Member
    Join Date
    Apr 2011
    Location
    Tucson, AZ
    Posts
    1,058
    Rep Power
    6

    Default Re: Array question

    Answer these questions and I bet you'll figure it out:

    1. Why is your array an int[] instead of a char[]?

    2. Why is your 'for' counter a char instead of an int?

    3. Why are you trying to write to index 97 when the array has only 26 elements?

    4. Will your 'for' condition ever be true?
    Last edited by kjkrum; 08-27-2012 at 11:26 AM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default Re: Array question

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    Jbo
    Jbo is offline Member
    Join Date
    Aug 2012
    Posts
    2
    Rep Power
    0

    Default Re: Array question

    Thanks kjkrum, I am currently looking at the code again.

Similar Threads

  1. Array question
    By Arom in forum New To Java
    Replies: 2
    Last Post: 11-24-2011, 02:28 PM
  2. Question about a 2d array example
    By silverglade in forum New To Java
    Replies: 8
    Last Post: 06-06-2011, 11:56 PM
  3. Question about array
    By hei1233212000 in forum New To Java
    Replies: 2
    Last Post: 09-18-2010, 03:55 PM
  4. array question
    By dazednconfused in forum New To Java
    Replies: 4
    Last Post: 09-15-2009, 05:44 AM
  5. Array question
    By McChill in forum New To Java
    Replies: 5
    Last Post: 02-20-2009, 02:18 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
  •