Results 1 to 18 of 18
  1. #1
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default need help with arrays

    Hi All

    I need some help please I am trying to create an array that holds all the letters of the alphabet and then prints them out. this is my code:

    public class Alphabet {

    public static void main (String [] args)
    {
    char[]myLetters = new char[26];

    for (char index = 0; index < myLetters.length; index++)
    {
    myLetters[index] = 97 ;

    }
    for (char index = 0; index < myLetters.length; index++)
    {
    System.out.println(myLetters[index]);
    }
    }

    }
    my output:

    is just a line of 26 b's.

    Where am I giong wrong?

  2. #2
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default

    you have to char++ somewhere
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  3. #3
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Wink simple ascii printable character set.

    Normally, it is better not to do this but the handy utility here is a good tool for beginners to have: It shows the base ascii printable characters.

    Java Code:
    // An array may not hold all the letters of the alphabet.
    // A print all ascii char utility. Save some beginners
    // several sluggish starts, use a StringBuffer
    
    public class AlphaZetaBet
    {
        // String Buffer - we do not know how far it will go.
        StringBuffer stringBuffer = new StringBuffer(26);
        String getCharacters()
        {
            // note the single tick marks for a char
            char c = ' ';// space, the beginning of printable
            // This stops on backspace character.
            do
            {
                stringBuffer.append(c++);
            }
            while(c < 0x7f);
            return stringBuffer.toString();
        }
        public static void main (String [] args)
        {
            AlphaZetaBet azb = new AlphaZetaBet();
            System.out.println(azb.getCharacters());
        }
       
    }
    Bye all!
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  4. #4
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    you have to char++ somewhere
    To expand on that, your for loop is assigning the number 97 to every slot in your array. As you can see, number 97 is the ASCII numerical value for b.
    Last edited by Jeremy; 07-05-2008 at 11:27 PM.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    myLetters[index] = 97 ;

    Why are you assigning 97 to each element in the array?

    Are you sure that b's are output and not a's?
    If you look at the binary value for the character b as decimal, it is 98, as hex 0x62
    'a's value in decimal is 97 or in hex is 0x61

    Why are you using the char type for an index in your for loops?
    Normally int is used.

    To put all the characters the alphabet in an array using a loop like you are doing requires that the values of the letters be contiquous. Then you must know the value of the first/smallest.
    The ascii letter values are not contiguous. They come in two ranges, one for lower case: a-z and one for upper case: A-Z. So you'd need two loops to put both lower and upper case letters.
    Since char can treated as an integer, ie you can do arithmetic on it, then the assignment statement in your loop that assigns a value to each element would be:

    array[index] = charValue++; // put in a char and incr value to next char

    charValue would start at 'a' for lower case and 'A' for upper case.
    Last edited by Norm; 07-05-2008 at 11:35 PM.

  6. #6
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi everyone who responded to my call for help.

    Norm: The value 97 was the ASCii for a but because I had +1 it turned the value to 98 which is b.

    Jeremy you said that I have to you have to char++ somewhere. I tiried everywhere. where do I use char++.

    changed the code just a bit.
    public class Alphabet {

    public static void main (String [] args)
    {
    char[]myLetters = new char[26];


    for (int index = 0; index < myLetters.length; index++)
    {
    myLetters[index] = 'a';


    }
    for (int index = 0; index < myLetters.length; index++)
    {
    System.out.println(myLetters[index]);
    index ++;


    }
    }

    }

  7. #7
    Jeremy is offline Member
    Join Date
    Jul 2008
    Posts
    28
    Rep Power
    0

    Default

    I stand corrected on 97 being b. :D

    Quote Originally Posted by Jman View Post
    Jeremy you said that I have to you have to char++ somewhere. I tiried everywhere. where do I use char++.
    I didn't. Norm did. ;)

    But, do you see what you're doing wrong here?

    Java Code:
    for (int index = 0; index < myLetters.length; index++) {
        myLetters[index] = 'a';
    }
    You are assigning the letter 'a' to every spot in the array. You need to increment that value somehow.

    Java Code:
    for (int index = 0; index < myLetters.length; index++) {
        myLetters[index] = 97 + index;
    }
    Explain to me what this loop is doing so I know you understand it. :cool:

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    Java Code:
     for (int index = 0; index < myLetters.length; index++)
    {
    System.out.println(myLetters[index]);
    index ++;
    }
    What happens to the value of index as you go thru this loop?
    it starts at 0, then is incremented by 1 at the bottom of the loop to 1 and then it is incremented by 1 to 2 in the for()
    I would expect this loop to output all the even positioned elements in the array.

  9. #9
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default char++

    Quote Originally Posted by Jeremy View Post
    To expand on that, (...snip...)
    Java Code:
    stringBuffer.append(c++);
    That should increment the character, Norm is an experienced worker at computer coding and knows how loops must be approached in a tediously detailed manner. I am swamped right now. If this does not do what I think it does then let Norm know, I will assist Norm.

    He is our guide. { yuk-yuk }
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  10. #10
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default auto-conversion

    Quote Originally Posted by Norm View Post
    Why are you using the char type for an index in your for loops?
    Normally int is used.
    ( ... and related matters .... )

    Norm, Java will often do an auto-conversion beyond the reach of the coder. It used to be that Java would back propogate the conversion in such a manner that compiler errors would show up somewhere else, leaving the beginner totally dumbfounded.

    I do not know if that happens today, or in this code.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    I hope the OP isn't throughly confused by now.

  12. #12
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi Jeremy

    Sorry for the late reply I was away.

    With regards to your question:
    Code:
    for (int index = 0; index < myLetters.length; index++) {
    myLetters[index] = 97 + index;
    }
    the above code states that for the first location in the array should be a b and every one after that could be +1 so cdefghij etc.

    I tired this code but it gives me a error message stating the following:

    possible loss of precision on line myLetters[index] = 97 + index;

  13. #13
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default use cast

    Quote Originally Posted by Jman View Post
    Sorry for the late reply I was away.
    Me too, working on sluggish machine.
    Quote Originally Posted by Jman View Post
    possible loss of precision ...
    Usually that is fixed with a cast:
    array[index] = (char) val;
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  14. #14
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default Quick exploring

    Hello JMan ;)

    If you run this code and ignore the PC speaker going nuts:
    Java Code:
    public class Main{
    	public static void main(String[] arg) {
    		for (int i = 0; i < 256; i++){
    			System.out.println(Integer.toString(i) + "\t=\t" + (char) i);
    		}
    	}
    }
    You will see a list of integers with their corresponding characters. Notice that integers where used since the Java byte goes from -128 to 127. Selecting the integers from 65 to 90 will give the alphabet. So the code will then be:
    Java Code:
    public class Main{
    	public static void main(String[] arg) {
    		char[] alphabet = new char[26];
    		for (int i = 65, counter = 0; i <= 90; i++){
    			alphabet[counter] = (char) i;
    			counter++;
    		}
    		for (char letter : alphabet){
    			System.out.print(letter + " ");
    		}
    		System.out.println();
    	}
    }
    Which gives output
    Java Code:
    A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
    I hope that helps you JMan :)
    Last edited by tim; 07-18-2008 at 10:08 PM.
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  15. #15
    Jman is offline Member
    Join Date
    Dec 2007
    Posts
    17
    Rep Power
    0

    Default

    Hi Nicholas and Tim

    first thank you both very much for all the help. Nicholas you said that array[index] = (char) val; if I used what you showed me here it could fix a possible loss of precision. I tried to use the logic in every what way and I gave in at the end.

    But thank you Tim you code does work so I want to see what is wrong with my code compared to yours.

  16. #16
    tim's Avatar
    tim
    tim is offline Senior Member
    Join Date
    Dec 2007
    Posts
    435
    Rep Power
    7

    Default

    Hello Jman ;)
    Quote Originally Posted by Jman View Post
    Hi Nicholas and Tim

    first thank you both very much for all the help. Nicholas you said that array[index] = (char) val; if I used what you showed me here it could fix a possible loss of precision. I tried to use the logic in every what way and I gave in at the end.

    But thank you Tim you code does work so I want to see what is wrong with my code compared to yours.
    Here is your code, except changed the blue part:
    Java Code:
    public class Main{
    	public static void main (String [] args) {
    		char[] myLetters = new char[26];
    
    		for ([COLOR="DarkOrchid"]char[/COLOR] index = 0; index < myLetters.length; index++)	{
    			myLetters[index] = [COLOR="RoyalBlue"](char) (97 + index)[/COLOR];
    		}
    		for (char index = 0; index < myLetters.length; index++)	{
    			System.out.println(myLetters[index]);
    		}
    	}
    }
    Yours just print lower case alphabet letters, but does essentially the same thing. I think your method is simpler though. I just wanted to rise to the challenge. It appears that variable index can be of type char, byte or int. Java does the converting for us. The problem was, you must cast to explicitly convert between certain primitive types. Actually, the privative types are "boxed" into their Object equivalents, casted, and then converted back to the correct primitives. This is called auto-boxing. And it makes our lives a lot easier. ;)

    I hope this clears everything up JMan. :D
    Eyes dwelling into the past are blind to what lies in the future. Step carefully.

  17. #17
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,317
    Rep Power
    25

    Default

    Why do you use the decimal number 97 when the char 'a' would be much clearer?

  18. #18
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Default loss of precision?

    Quote Originally Posted by Jman View Post
    ....possible loss of precision.....
    Tim shows the correct code, at least that's the way I would do it and skip the auto-boxing.

    What loss of precsions means is thus:

    110111 100001 <- One datatype.

    11010000011000111100 <- Another datatype

    The big one won't fit in the little one. Compiler warns you if you do it. Placing (char) at the point shown tells compiler you are aware of the loss.
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

Similar Threads

  1. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 02:24 AM
  2. new to arrays
    By jimJohnson in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 02:45 PM
  3. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 10:08 PM
  4. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM
  5. Problems with arrays
    By Marcus in forum New To Java
    Replies: 2
    Last Post: 07-04-2007, 08:10 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
  •