By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,665 Members | 1,430 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,665 IT Pros & Developers. It's quick & easy.

alphabet array

P: 62
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void GetAlphabet (char alphabet[]) { 
  3.        int index, ordinalVal = 97; 
  4.        char element;  
  5.  
  6.  
  7.        for (index = 1; index < alphabet.length; ++index, ++ordinalVal) {
  8.            element = (char) ordinalVal; 
  9.            System.out.println("Letter: " + element); 
  10.            alphabet[index] = element;
  11.            System.out.println("Letter: " + alphabet[index]); 
  12.  
  13.        }  
  14.  
The first print will display the letter but after attempting to add it to the array the second print wont work?
Oct 29 '07 #1
Share this Question
Share on Google+
20 Replies


10K+
P: 13,264
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.

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void GetAlphabet (char alphabet[]) { 
  3.        int index, ordinalVal = 97; 
  4.        char element;  
  5.  
  6.  
  7.        for (index = 1; index < alphabet.length; ++index, ++ordinalVal) {
  8.            element = (char) ordinalVal; 
  9.            System.out.println("Letter: " + element); 
  10.            alphabet[index] = element;
  11.            System.out.println("Letter: " + alphabet[index]); 
  12.  
  13.        }  
  14.  
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.
Oct 29 '07 #2

P: 62
Are u saying instead of using an 'int index' i should make it a 'char index' starting at char 'a'?
Oct 29 '07 #3

10K+
P: 13,264
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.
Oct 29 '07 #4

P: 62
Ok i have this:
Expand|Select|Wrap|Line Numbers
  1.  public static void GetAlphabet (char alphabet[]) { 
  2.        int ordinalVal = 97; 
  3.        char index, element;  
  4.  
  5.  
  6.        for (index = 'a'; index <= 'z'; ++index, ++ordinalVal) {
  7.            element = (char) ordinalVal; 
  8.            System.out.println("Letter: " + element); 
  9.            alphabet[index] = element;
  10.            System.out.println("Letter: " + alphabet[index]); 
  11.  
  12.        }   
  13.  
But i get error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 97
at Main.GetAlphabet(Main.java:38)
at Main.main(Main.java:19)
Java Result: 1
Oct 29 '07 #5

P: 62
AH k, i just think theres a problem with storing all the letters in the array cause i dont think they are being stored.
Oct 29 '07 #6

10K+
P: 13,264
Ok i have this:
Expand|Select|Wrap|Line Numbers
  1.  public static void GetAlphabet (char alphabet[]) { 
  2.        int ordinalVal = 97; 
  3.        char index, element;  
  4.  
  5.  
  6.        for (index = 'a'; index <= 'z'; ++index, ++ordinalVal) {
  7.            element = (char) ordinalVal; 
  8.            System.out.println("Letter: " + element); 
  9.            alphabet[index] = element;
  10.            System.out.println("Letter: " + alphabet[index]); 
  11.  
  12.        }   
  13.  
But i get error:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 97
at Main.GetAlphabet(Main.java:38)
at Main.main(Main.java:19)
Java Result: 1
Expand|Select|Wrap|Line Numbers
  1. int counter = 0;
  2.        for (index = 'a'; index <= 'z'; index++, counter++) {
  3.                      alphabet[counter] = index;
  4.  // .....
Oct 29 '07 #7

Expert 10K+
P: 11,448
Expand|Select|Wrap|Line Numbers
  1. int counter = 0;
  2.        for (index = 'a'; index <= 'z'; index++, counter++) {
  3.                      alphabet[counter] = index;
  4.  // .....
Look ma! No counter variable!

Expand|Select|Wrap|Line Numbers
  1. for (char index= 'a'; index <= 'z'; index++)
  2.    alphabet[index-'a']= index;
  3.  
Look ma! No loop!

Expand|Select|Wrap|Line Numbers
  1. char[] alphabet= "abcdefghijklmnopqrstuvwxyz".toCharArray();
  2.  
kind regards,

Jos ;-)
Oct 29 '07 #8

10K+
P: 13,264
Look ma! No counter variable!

Expand|Select|Wrap|Line Numbers
  1. for (char index= 'a'; index <= 'z'; index++)
  2.    alphabet[index-'a']= index;
  3.  
Look ma! No loop!

Expand|Select|Wrap|Line Numbers
  1. char[] alphabet= "abcdefghijklmnopqrstuvwxyz".toCharArray();
  2.  
kind regards,

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

P.S Where were you?
Oct 29 '07 #9

Expert 10K+
P: 11,448
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
Oct 29 '07 #10

P: 62
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void main(String[] args) {
  3.  
  4.        String sentence = new String();
  5.        char alphabet[] = new char [26]; 
  6.  
  7.        GetString(sentence);
  8.        GetAlphabet(alphabet);
  9.  
  10.     }
  11.    public static void GetString (String sentence) { 
  12.  
  13.        System.out.println("Please enter a string: "); 
  14.        sentence = Keybd.in.readLine(); 
  15.  
  16.        return; 
  17.  
  18.    } 
  19.  
  20.    public static void GetAlphabet (char alphabet[]) { 
  21.  
  22.        alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
  23.  
  24.      return;
  25.    }
  26.  
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.print to display the string and array index in mian it wont show anyrthing?
Oct 30 '07 #11

10K+
P: 13,264
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?

Expand|Select|Wrap|Line Numbers
  1.  
  2. public static void main(String[] args) {
  3.  
  4.        String sentence = new String();
  5.        char alphabet[] = new char [26]; 
  6.  
  7.        GetString(sentence);
  8.        GetAlphabet(alphabet);
  9.  
  10.     }
  11.    public static void GetString (String sentence) { 
  12.  
  13.        System.out.println("Please enter a string: "); 
  14.        sentence = Keybd.in.readLine(); 
  15.  
  16.        return; 
  17.  
  18.    } 
  19.  
  20.    public static void GetAlphabet (char alphabet[]) { 
  21.  
  22.        alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
  23.  
  24.      return;
  25.    }
  26.  
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.print 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.
Oct 30 '07 #12

P: 62
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.
Oct 30 '07 #13

10K+
P: 13,264
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.
Oct 30 '07 #14

P: 62
sorry it cant find symbol in main but thats where i declared it?
Oct 30 '07 #15

10K+
P: 13,264
sorry it cant find symbol in main but thats where i declared it?
Perhaps if you post what you have we can see you are talking about now.

P.S: The error messages usually give you some hints on how to solve the problem.
Oct 30 '07 #16

P: 62
Here my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  public static void main(String[] args) {
  3.         int index =5;
  4.        String sentence = new String();
  5.        char alphabet[] = new char [26]; 
  6.  
  7.        GetString(sentence);
  8.        GetAlphabet(alphabet);
  9.        System.out.println("Letter 5: " + alphabet[index]); //Here the f wont be printed because i dont think the data is being stored in the array, samething for the string.
  10.        StoreCharacters(sentence, alphabet);
  11.     }
  12.    static void GetString (String sentence) { 
  13.  
  14.        System.out.println("Please enter a string: "); 
  15.        sentence = Keybd.in.readLine(); 
  16.  
  17.        return; 
  18.  
  19.    } 
  20.  
  21.    static void GetAlphabet (char alphabet[]) { 
  22.        int index = 5;
  23.        alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
  24.         System.out.println("Letter 5: " + alphabet[index]); // This will print f 
  25.      return;
  26.    } 
  27.  
  28.  
Oct 30 '07 #17

10K+
P: 13,264
Here my code:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  public static void main(String[] args) {
  3.         int index =5;
  4.        String sentence = new String();
  5.        char alphabet[] = new char [26]; 
  6.  
  7.        GetString(sentence);
  8.        GetAlphabet(alphabet);
  9.        System.out.println("Letter 5: " + alphabet[index]); //Here the f wont be printed because i dont think the data is being stored in the array, samething for the string.
  10.        StoreCharacters(sentence, alphabet);
  11.     }
  12.    static void GetString (String sentence) { 
  13.  
  14.        System.out.println("Please enter a string: "); 
  15.        sentence = Keybd.in.readLine(); 
  16.  
  17.        return; 
  18.  
  19.    } 
  20.  
  21.    static void GetAlphabet (char alphabet[]) { 
  22.        int index = 5;
  23.        alphabet= "abcdefghijklmnopqrstuvwxyz ".toCharArray(); 
  24.         System.out.println("Letter 5: " + alphabet[index]); // This will print f 
  25.      return;
  26.    } 
  27.  
  28.  
Don't you think that a getString method should return a String?

Expand|Select|Wrap|Line Numbers
  1.     static String getString () { 
  2.                         System.out.println("Please enter a string: "); 
  3.                  String sentence = Keybd.in.readLine(); 
  4.                         return sentence; 
  5.        } 
And you can then get the String in your main method using
Expand|Select|Wrap|Line Numbers
  1. String sentence = getString();
Do the same thing for the char[].
Oct 30 '07 #18

P: 62
i see what you are saying but i still get these errors: Ive declared the string in main?


Expand|Select|Wrap|Line Numbers
  1.  
  2.  public static String GetString () { 
  3.        System.out.println("Please enter a string: "); 
  4.        sentence = Keybd.in.readLine(); // // Cannot Find symbol, variable sentence Location class Main
  5.  
  6.        return sentence;  // Cannot Find symbol, variable sentence Location class Main
  7.  
Oct 30 '07 #19

10K+
P: 13,264
i see what you are saying but i still get these errors: Ive declared the string in main?


Expand|Select|Wrap|Line Numbers
  1.  
  2.  public static String GetString () { 
  3.        System.out.println("Please enter a string: "); 
  4.        sentence = Keybd.in.readLine(); // // Cannot Find symbol, variable sentence Location class Main
  5.  
  6.        return sentence;  // Cannot Find symbol, variable sentence Location class Main
  7.  
Read the code in my previous reply. It's different from what you wrote.
Oct 30 '07 #20

P: 62
ops i see, ah thanks for the help
Oct 30 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.