1 2 Previous Next 16 Replies Latest reply: Dec 23, 2006 4:21 PM by 796334 RSS

    help with char Array..must put letters in the array (instead of numbers)

    807599
      im supposed to make a small program where i have to put letters (in alphabetical order in a char array) i kno how to put numbers
      (i can put 1-5 in an array) in an array..but i dont know how to do it for characters...i post code..any comments would me most appreciated..
       public static void initialize (char [] list) {
          for (int i = 0; i < list.length; i++) { 
            list[i] += 65;
          }
        }
      if this was an int array list[i] = i +1 (if i was supposed to put numbers in the array) but since this is chars i really dont know how to do this..i tried to put unicode (list [ i ] += 65..("A" for those who dont know) but it doesnt work..im getting question marks, and other weird symbols..
        • 1. Re: help with char Array..must put letters in the array (instead of numbers
          807599
          i tried this in a new program without the use of methods..
          public class PuttingLettersinCharArray {
            public static void main (String [] args) {
              char [] list = new char [5];
              for (int i = 0; i < list.length; i++) {
                list[i] += 97 + i;
                System.out.print(list);
          }
          System.out.println();
          }
          }

          this one seems to work but i still dont get why my original code doesn't work
          • 2. Re: help with char Array..must put letters in the array (instead of numbers
            791266
            @Op. What do you mean? Your original code works.
            public class Test {
                 public static void main(String[] arguments) {
                      char[] data = new char[20];
                      initialize(data);
                      System.out.println(new String(data));
                 }
            
                 public static void initialize(char[] list) {
                      for (int i = 0; i < list.length; i++) {
                           list[i] += 65;
                      }
                 }
            }
            Prints:
            AAAAAAAAAAAAAAAAAAAA

            Kaj
            • 3. Re: help with char Array..must put letters in the array (instead of numbers
              791266
              Just change the line:
              list[i] += 65;
              to
              list[i] += 65 + i;
              If you want the alphabet.

              Kaj
              • 4. Re: help with char Array..must put letters in the array (instead of numbers
                807599
                u mean like this..
                public static void initialize (char [] list) {
                   for (int i = 0; i < list.length; i++) {
                      list[i] +=  65 + i;
                    }
                  }
                i tried it but its outputting this..(ignore square brackets)

                [?] [?] [?] [?] [?] [?]

                this is a method and im supposed to output A, B, C etc..i have another program that i run off to make this work..

                Message was edited by:
                moonmaster
                • 5. Re: help with char Array..must put letters in the array (instead of numbers
                  791266
                  u mean like this..
                  public static void initialize (char [] list) {
                  for (int i = 0; i < list.length; i++) {
                  list[i] +=  65 + i;
                  }
                  }
                  i tried it but its outputting this..(ignore square
                  brackets)

                  [?] [?] [?] [?] [?] [?]

                  this is a method and im supposed to output A, B, C
                  etc..i have another program that i run off to make
                  this work..

                  Message was edited by:
                  moonmaster
                  I don't know what you are doing, this.
                  public class Test {
                       public static void main(String[] arguments) {
                            char[] data = new char[26];
                            initialize(data);
                            System.out.println(new String(data));
                       }
                  
                       public static void initialize(char[] list) {
                            for (int i = 0; i < list.length; i++) {
                                 list[i] += 65 + i;
                            }
                       }
                  }
                  Prints:
                  ABCDEFGHIJKLMNOPQRSTUVWXYZ

                  (Using 'A' instead of 65 makes it easier to understand, so I think you should change your code)

                  Kaj
                  • 6. Re: help with char Array..must put letters in the array (instead of numbers
                    807599
                    i dont think u understand what im supposed to do...okay i need to make a METHOD called initialize that works for an array of chars..This initialize method should fill the array with characters A, B, C etc..Do u know what method overloading is? This is it..i'll show you a sample METHOD..(there's a reason why i put method in capitals)
                    public static void initialize (int [] list) {
                        for (int i = 0; i < list.length; i++) {
                          list[i] = i+1;
                        }
                      }
                    This is a method where i have an array of ints as a parameter and it prints out 1, 2, 3 etc.the difference is i need to make a METHOD that prints out A, B, C etc but it doesnt work..i hope u see my problem..
                    • 7. Re: help with char Array..must put letters in the array (instead of numbers
                      807599
                      so in the example..i have another program that has the following lines
                      int[] test1 = new int[8];
                      Utilities.initialize(test1);
                      Utilities.printArray(test1);
                      printLine();

                      so this runs and reads off my program file, giving me my output (of course there's another method for printArray that prints it out..) do u understand..

                      Message was edited by:
                      moonmaster
                      • 8. Re: help with char Array..must put letters in the array (instead of numbers
                        807599
                        What not to understand? you have been given the answer, or at least enough code to find the answer. Well, OK, maybe you are slow, or lazy, but here is the code I believe you are looking for, it is Christmas, after all!
                        public class Test{
                             
                        
                             public static void main(String[] args) throws Exception {
                                  
                                  int[] intArr = new int[10];
                                  char[] charArr = new char[26];
                                  initialize(intArr);
                                  printArray(intArr);
                                  initialize(charArr);
                                  printArray(charArr);
                                  
                                  
                                  
                             }
                             
                             private static void initialize(int [] list)
                             {
                                  for(int i = 0; i < list.length; i++)
                                  {
                                       list[i] = i;
                                       
                                  }
                                  
                             }
                             
                             private static void initialize(char[] list)
                             {
                                  for(int i = 0; i < list.length; i++)
                                  {
                                       list[i] = (char)('A' + i);
                                       
                                  }
                                  
                             }
                        
                             private static void printArray(int[] list)
                             {
                                  for(int i = 0; i < list.length; i++)
                                  {
                                       System.out.print(list);
                                  }
                                  System.out.println();
                             }

                             private static void printArray(char[] list)
                             {
                                  for(int i = 0; i < list.length; i++)
                                  {
                                       System.out.print(String.valueOf(list[i]));
                                  }
                                  System.out.println();
                             }
                        }



                        ~Tim

                        MERRY CHRISTMAS EVERYONE!!
                        • 9. Re: help with char Array..must put letters in the array (instead of numbers
                          807599
                          public static char[] initialize () {
                              char[] list = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".toCharArray();
                              return list;
                          }
                          • 10. Re: help with char Array..must put letters in the array (instead of numbers
                            807599
                            unfortunately..this is not what im looking for..thx anyway guys..i'll guess i 'll keep trying..
                            • 11. Re: help with char Array..must put letters in the array (instead of numbers
                              807599
                              then you need to explain what it is you are trying to do a whole lot better. I have given you a method tha initializes an int array with the number 0 thru 9, and a method that prints out that array.

                              I also included another method initialize that accepts a char [] (thus overloading the initialize method) and a last method that prints out the char array. (overloading the printArray(int[]) method)

                              ~Tim
                              • 12. Re: help with char Array..must put letters in the array (instead of numbers
                                791266
                                unfortunately..this is not what im looking for..thx
                                anyway guys..i'll guess i 'll keep trying..
                                You are in that case really bad in explaining what you want.
                                • 13. Re: help with char Array..must put letters in the array (instead of numbers
                                  494490
                                  You are in that case really bad in explaining what
                                  you want.
                                  It might help if he explained it in capital letters, like 'METHOD.'
                                  • 14. Re: help with char Array..must put letters in the array (instead of numbers
                                    807599
                                    Maybe he meant that the METHOD's were supposed to be in another class. If thats the case and he couldn't take my code and use it than he needs to find a new career path.
                                    //MY TESTING CLASS
                                    
                                    public class Test{
                                         
                                          
                                         public static void main(String[] args) throws Exception {
                                              
                                              int[] intArr = new int[10];
                                              char[] charArr = new char[26];
                                              Utilities.initialize(intArr);
                                              Utilities.printArray(intArr);
                                              Utilities.initialize(charArr);
                                              Utilities.printArray(charArr);
                                              
                                              
                                              
                                         }
                                         
                                    
                                    }
                                     
                                    
                                    //MY UTILITIES CLASS
                                    
                                    public class Utilities {
                                    
                                         public static void initialize(int [] list)
                                         {
                                              for(int i = 0; i < list.length; i++)
                                              {
                                                   list[i] = i;
                                                   
                                              }
                                              
                                         }
                                         
                                         static void initialize(char[] list)
                                         {
                                              for(int i = 0; i < list.length; i++)
                                              {
                                                   list[i] = (char)('A' + i);
                                                   
                                              }
                                              
                                         }
                                     
                                         static void printArray(int[] list)
                                         {
                                              for(int i = 0; i < list.length; i++)
                                              {
                                                   System.out.print(list);
                                              }
                                              System.out.println();
                                         }

                                         static void printArray(char[] list)
                                         {
                                              for(int i = 0; i < list.length; i++)
                                              {
                                                   System.out.print(String.valueOf(list[i]));
                                              }
                                              System.out.println();
                                         }
                                    }



                                    ~Tim
                                    1 2 Previous Next