473,239 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,239 software developers and data experts.

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.

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
20 18973
r035198x
13,262 8TB
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
Are u saying instead of using an 'int index' i should make it a 'char index' starting at char 'a'?
Oct 29 '07 #3
r035198x
13,262 8TB
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
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
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
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
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
r035198x
13,262 8TB
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
JosAH
11,448 Expert 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
sorry it cant find symbol in main but thats where i declared it?
Oct 30 '07 #15
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
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
r035198x
13,262 8TB
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
ops i see, ah thanks for the help
Oct 30 '07 #21

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

Similar topics

5
by: skinnybloke | last post by:
Hi I am trying to do something with PHP and having problems - a bit of a a PHP novice I'm afraid. What I am trying to achieve is to display the alphabet and numbers 0-9 on a web page. I want...
5
by: Stefan Krah | last post by:
Hello, I am currently writing code where it is convenient to convert char to int . The conversion function relies on a character set with contiguous alphabets. int set_mesg(Key *key, char...
12
by: one | last post by:
greetings i am just wondering if some expert here can either show me how to do this or point me to the right direction (url... i want to use c# to generate a list of alphabet e.g A B C ... AA...
2
by: steve smith | last post by:
Hi I am new to this language, and still don't understand most of the concepts, I am trying to sort an arraylist by alphabet, I belive i need to use the IComparer interface, but have no idea how...
8
by: Jack Addington | last post by:
I want to scroll through the alphabet in order to scroll some data to the closest name that starts with a letter. If the user hits the H button then it should scroll to the letter closest to H. ...
9
by: booksnore | last post by:
I am writing some code to search for strings that contain every letter of the alphabet. At the moment I am using the method below to check to see if a string contains every letter of the alphabet....
31
by: Joe Smith | last post by:
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789" " " "!#%^&*()-_" "+=~\|;:\'" "\"{},.<>/\?" "\a\b\f\n\r\t\v\\" Do the above string literals comprise an alphabet for C?...
0
by: rockdale | last post by:
Hi, all: I implemented an alphabet list so that when user click letter A then I will re-bind a gridview control using SQL stored procedure. It works fine. The problem is I populate my alphabet...
1
by: kannabiran | last post by:
Hi, Im using C# ASP.Net here in the textbox i need to get the input as like follows ,any alphabet or any alphabet,any alphabet for example ,C or C,E like this i want to get the input.The...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.