alphabet array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geebanga88
    New Member
    • Aug 2007
    • 62

    alphabet array

    HI i have a method that is supose to store the alphabet in an array however dont think that it is being added to the array.

    Code:
     
    public static void GetAlphabet (char alphabet[]) { 
           int index, ordinalVal = 97; 
           char element;  
         
           
           for (index = 1; index < alphabet.length; ++index, ++ordinalVal) {
               element = (char) ordinalVal; 
               System.out.println("Letter: " + element); 
               alphabet[index] = element;
               System.out.println("Letter: " + alphabet[index]); 
               
           }
    The first print will display the letter but after attempting to add it to the array the second print wont work?
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by geebanga88
    HI i have a method that is supose to store the alphabet in an array however dont think that it is being added to the array.

    Code:
     
    public static void GetAlphabet (char alphabet[]) { 
           int index, ordinalVal = 97; 
           char element;  
         
           
           for (index = 1; index < alphabet.length; ++index, ++ordinalVal) {
               element = (char) ordinalVal; 
               System.out.println("Letter: " + element); 
               alphabet[index] = element;
               System.out.println("Letter: " + alphabet[index]); 
               
           }
    The first print will display the letter but after attempting to add it to the array the second print wont work?
    See also this thread.

    Comment

    • geebanga88
      New Member
      • Aug 2007
      • 62

      #3
      Are u saying instead of using an 'int index' i should make it a 'char index' starting at char 'a'?

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by geebanga88
        Are u saying instead of using an 'int index' i should make it a 'char index' starting at char 'a'?
        Just pointing out a thread where the alphabet was printed that's all.

        Comment

        • geebanga88
          New Member
          • Aug 2007
          • 62

          #5
          Ok i have this:
          Code:
           public static void GetAlphabet (char alphabet[]) { 
                 int ordinalVal = 97; 
                 char index, element;  
               
                 
                 for (index = 'a'; index <= 'z'; ++index, ++ordinalVal) {
                     element = (char) ordinalVal; 
                     System.out.println("Letter: " + element); 
                     alphabet[index] = element;
                     System.out.println("Letter: " + alphabet[index]); 
                     
                 }
          But i get error:
          Exception in thread "main" java.lang.Array IndexOutOfBound sException: 97
          at Main.GetAlphabe t(Main.java:38)
          at Main.main(Main. java:19)
          Java Result: 1

          Comment

          • geebanga88
            New Member
            • Aug 2007
            • 62

            #6
            AH k, i just think theres a problem with storing all the letters in the array cause i dont think they are being stored.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by geebanga88
              Ok i have this:
              Code:
               public static void GetAlphabet (char alphabet[]) { 
                     int ordinalVal = 97; 
                     char index, element;  
                   
                     
                     for (index = 'a'; index <= 'z'; ++index, ++ordinalVal) {
                         element = (char) ordinalVal; 
                         System.out.println("Letter: " + element); 
                         alphabet[index] = element;
                         System.out.println("Letter: " + alphabet[index]); 
                         
                     }
              But i get error:
              Exception in thread "main" java.lang.Array IndexOutOfBound sException: 97
              at Main.GetAlphabe t(Main.java:38)
              at Main.main(Main. java:19)
              Java Result: 1
              [CODE=java]int counter = 0;
              for (index = 'a'; index <= 'z'; index++, counter++) {
              alphabet[counter] = index;
              // .....[/CODE]

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by r035198x
                [CODE=java]int counter = 0;
                for (index = 'a'; index <= 'z'; index++, counter++) {
                alphabet[counter] = index;
                // .....[/CODE]
                Look ma! No counter variable!

                [code=java]
                for (char index= 'a'; index <= 'z'; index++)
                alphabet[index-'a']= index;
                [/code]

                Look ma! No loop!

                [code=java]
                char[] alphabet= "abcdefghijklmn opqrstuvwxyz".t oCharArray();
                [/code]

                kind regards,

                Jos ;-)

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by JosAH
                  Look ma! No counter variable!

                  [code=java]
                  for (char index= 'a'; index <= 'z'; index++)
                  alphabet[index-'a']= index;
                  [/code]

                  Look ma! No loop!

                  [code=java]
                  char[] alphabet= "abcdefghijklmn opqrstuvwxyz".t oCharArray();
                  [/code]

                  kind regards,

                  Jos ;-)
                  Don't start chasing all the kids away again ...

                  P.S Where were you?

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by r035198x
                    Don't start chasing all the kids away again ...

                    P.S Where were you?
                    Sorry 'squire; I was out of my office: contract hunting ;-)

                    kind regards,

                    Jos

                    Comment

                    • geebanga88
                      New Member
                      • Aug 2007
                      • 62

                      #11
                      Ah thank for the help Josh much better without a loop:)

                      i just have one more problem.
                      I have to methods that get the data for a string and the array. But when i return to main i dont think the data put in by the methods is stored?

                      Code:
                       
                      public static void main(String[] args) {
                          
                             String sentence = new String();
                             char alphabet[] = new char [26]; 
                             
                             GetString(sentence);
                             GetAlphabet(alphabet);
                             
                          }
                         public static void GetString (String sentence) { 
                             
                             System.out.println("Please enter a string: "); 
                             sentence = Keybd.in.readLine(); 
                             
                             return; 
                             
                         } 
                      
                         public static void GetAlphabet (char alphabet[]) { 
                             
                             alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
                             
                           return;
                         }
                      Not sure the array and string dont return the data because arent all complex data types stored by reference? If i had a a system.out.prin t to display the string and array index in mian it wont show anyrthing?

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by geebanga88
                        Ah thank for the help Josh much better without a loop:)

                        i just have one more problem.
                        I have to methods that get the data for a string and the array. But when i return to main i dont think the data put in by the methods is stored?

                        Code:
                         
                        public static void main(String[] args) {
                            
                               String sentence = new String();
                               char alphabet[] = new char [26]; 
                               
                               GetString(sentence);
                               GetAlphabet(alphabet);
                               
                            }
                           public static void GetString (String sentence) { 
                               
                               System.out.println("Please enter a string: "); 
                               sentence = Keybd.in.readLine(); 
                               
                               return; 
                               
                           } 
                        
                           public static void GetAlphabet (char alphabet[]) { 
                               
                               alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
                               
                             return;
                           }
                        Not sure the array and string dont return the data because arent all complex data types stored by reference? If i had a a system.out.prin t to display the string and array index in mian it wont show anyrthing?
                        Java passes everything by value.
                        You should have your methods returning values and then in your main method you should be storing those values in some values. Both methods don't need to take any parameters.

                        P.S I hope you didn't use Jos' method as a excuse to avoid learning loops. He posted so that you can see all the possible (and best ) methods of doing it but you should know and understand them all if you want to learn Java properly.

                        Comment

                        • geebanga88
                          New Member
                          • Aug 2007
                          • 62

                          #13
                          Can u provide an example using that code, im getting confused because if i take away the parameters netbeans tells me that it cannot find the symbol "sentence" in the methods.

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by geebanga88
                            Can u provide an example using that code, im getting confused because if i take away the parameters netbeans tells me that it cannot find the symbol "sentence" in the methods.
                            Then you have to declare those variables in those methods.

                            Comment

                            • geebanga88
                              New Member
                              • Aug 2007
                              • 62

                              #15
                              sorry it cant find symbol in main but thats where i declared it?

                              Comment

                              Working...