473,323 Members | 1,551 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,323 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 18980
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...
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
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.