Results 1 to 20 of 21
Thread: help with arrays
- 04-14-2011, 12:42 AM #1
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
help with arrays
i am learning about arrays, and have this assignment:
Counts the frequencies of letters A-Za-z in a string
@param str a string
@return an array of 26 counts. The i-th count is the number of occurrences
of 'A' + i or 'a' + i.
and here is my coding so far:
i am thinking that i need to update my "i" for both my alphabet array, and the string, charAt.Java Code:int[]count = new int[26]; int counter = 1; int number = 0; char[]alphabet = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; for (int i = 0; i < str.length(); i++) { if (Character.isLetter(str.charAt(i))) { counter++; while (alphabet[i] == str.charAt(i)) { counter++; } } count[i] = counter; } return count;
- 04-14-2011, 12:46 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
What errors are you getting?(if compiler errors copy and paste the exact message, if logical show output and expected output)
Also, be sure to ask specific questions.
- 04-14-2011, 12:48 AM #3
You are making this harder than it needs to be. A little hint:
Java Code:char c = 'a'; String s = "abz"; for(int index = 0; index < s.length(); index++) { System.out.println((int) s.charAt(index) - c); }
- 04-14-2011, 12:56 AM #4
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
ok, so i put in a string, and will get consecutive numbers, such as 2, 3, 5, 6, 6, 7.
and thanks for the quick response!
what is the "- c"? i am confused, because i would think that that would be a number.
i get 0, 1, 25?
- 04-14-2011, 01:06 AM #5
- 04-14-2011, 01:12 AM #6
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
oh, ok... those are just the indexs then. i'll see what i can do with this...
- 04-15-2011, 04:41 PM #7
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
how about this for pseudocode?:
Java Code:Where index number (found from code above) = number of index in array, update counter (+1) for that array slot
- 04-17-2011, 08:04 PM #8
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
got it! here is what i have:
output (for hello):Java Code:char c = 'a'; int test = 0; int[]letters = new int[26]; int counter = 0; for(int index = 0; index < str.length(); index++) { test = ((int) str.charAt(index) - c); letters[test] = letters[test]+1; } for (int index=0; index < 26; index++) { System.out.print(letters[index] + ", "); }how do i remove the last comma?Java Code:0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
Last edited by droidus; 04-17-2011 at 08:07 PM.
- 04-18-2011, 12:25 AM #9
Your current algorithm is to print a number followed by a comma, except the last one. The problem is how do you know when you get to the last one.
A better and easier algorithm is to print a comma before the number except the first one.
- 04-18-2011, 01:36 AM #10
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
with
i get:Java Code:for (int index=0; index < 26; index++) { counter++; if (counter == 1) { System.out.print(letters[index] + ", "); } else { System.out.print(", " + letters[index]); } }
Java Code:0, , 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 2, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
- 04-18-2011, 01:50 AM #11
You need to refine the algorithm. You always want to print the number so remove that from the if statement.
By the way you can use the loop control variable for this and get rid of the counter.Java Code:loop { if not first number { print comma } print number
- 04-18-2011, 02:21 PM #12
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
when i run this, and return letters, i get some weird string. here is my code, and my output:
Java Code:char c = 'a'; int test = 0; int[]letters = new int[26]; int counter = 0; for(int index = 0; index < str.length(); index++) { test = ((int) str.charAt(index) - c); // Gets the string letter position in the alphabet letters[test] = letters[test]+1; // Gets the position in the array, and adds one to that index value }what is going on here??Java Code:[I@3e25a5
- 04-18-2011, 04:00 PM #13
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
I believe that's outputting a memory address (usually because you're not indexing the array correctly)
I'm at work at the moment (so can't look too much in to it), if nobody has responded with a fix before I get home (a couple of hrs) then I'll have a look at it then
- 04-18-2011, 04:33 PM #14
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
it sounds like it's the uppercase "H" for hello. i get an java.lang.ArrayIndexOutOfBoundsException error.
- 04-18-2011, 05:04 PM #15
Member
- Join Date
- Apr 2011
- Posts
- 12
- Rep Power
- 0
- 04-18-2011, 05:20 PM #16
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
That has nothing to do with it; that simple calculation assumes that the characters are in the range 'a' ... 'z'. If you subtract an 'a' from the character the index value is in the range 0 ... 25. An uppcase character is out of that range so the result is a negative index value which results in an AIOOBE.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-18-2011, 07:38 PM #17
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
ok. so how would i get around this? would i just have to use a switch statement?
- 04-18-2011, 07:50 PM #18
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,408
- Blog Entries
- 7
- Rep Power
- 17
Nope, a switch statement is so old fashioned; think about it: do you just want statistics (frequencies) of lower case letters? If so, you have to exclude any other character from your input. If not, you either have to use a much larger array (where every possible character can be a index) or abandon the array approach entirely. The choice is up to you.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
- 04-18-2011, 10:05 PM #19
Senior Member
- Join Date
- Feb 2011
- Posts
- 235
- Rep Power
- 3
i have to return an array, and it can only have 26 values (for each letter).
- 04-19-2011, 12:11 AM #20
Similar Threads
-
how to ask how many arrays you want
By jepoy in forum New To JavaReplies: 5Last Post: 09-06-2010, 03:19 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM -
Need help regarding Arrays Help pls....
By shaggyoo7 in forum New To JavaReplies: 3Last Post: 01-14-2009, 04:36 AM -
arrays
By hasysf in forum New To JavaReplies: 12Last Post: 07-28-2008, 02:38 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks