Page 1 of 2 12 LastLast
Results 1 to 20 of 21
  1. #1
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default 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:
    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;
    i am thinking that i need to update my "i" for both my alphabet array, and the string, charAt.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    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.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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);
    }

  4. #4
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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?

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    Quote Originally Posted by droidus View Post
    what is the "- c"? i am confused, because i would think that that would be a number.
    i get 0, 1, 25?
    Exactly. For 'a' the result is 0, for 'b' it is 1, for 'c' it is 2, ..... for 'z' it is 25. Do those numbers ring any bells? Array indicies perhaps.

  6. #6
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    oh, ok... those are just the indexs then. i'll see what i can do with this...

  7. #7
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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

  8. #8
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    got it! here is what i have:

    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] + ", ");
    	}
    output (for hello):
    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,
    how do i remove the last comma?
    Last edited by droidus; 04-17-2011 at 08:07 PM.

  9. #9
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    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.

  10. #10
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    with

    Java Code:
    for (int index=0; index < 26; index++) {
    	counter++;
    		if (counter == 1) {
    			System.out.print(letters[index] + ", ");
    		}
    		else {
    			System.out.print(", " + letters[index]);
    		}
    	}
    i get:

    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

  11. #11
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You need to refine the algorithm. You always want to print the number so remove that from the if statement.
    Java Code:
    loop {
        if not first number {
            print comma
        }
        print number
    By the way you can use the loop control variable for this and get rid of the counter.

  12. #12
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    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
    	}
    Java Code:
    [I@3e25a5
    what is going on here??

  13. #13
    ivlatt is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    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

  14. #14
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    it sounds like it's the uppercase "H" for hello. i get an java.lang.ArrayIndexOutOfBoundsException error.

  15. #15
    ivlatt is offline Member
    Join Date
    Apr 2011
    Posts
    12
    Rep Power
    0

    Default

    Quote Originally Posted by droidus View Post
    it sounds like it's the uppercase "H" for hello. i get an java.lang.ArrayIndexOutOfBoundsException error.
    Java Code:
    for(int index = 0; index < str.length(); index++)
    To do with that perhaps?

    .length results the total number of indexes in the array. Although your last index would be .length-1 (remember - because an array starts from position 0)

  16. #16
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by ivlatt View Post
    Java Code:
    for(int index = 0; index < str.length(); index++)
    To do with that perhaps?

    .length results the total number of indexes in the array. Although your last index would be .length-1 (remember - because an array starts from position 0)
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  17. #17
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    ok. so how would i get around this? would i just have to use a switch statement?

  18. #18
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,004
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by droidus View Post
    ok. so how would i get around this? would i just have to use a switch statement?
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  19. #19
    droidus is offline Senior Member
    Join Date
    Feb 2011
    Posts
    244
    Rep Power
    4

    Default

    i have to return an array, and it can only have 26 values (for each letter).

  20. #20
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

Page 1 of 2 12 LastLast

Similar Threads

  1. how to ask how many arrays you want
    By jepoy in forum New To Java
    Replies: 5
    Last Post: 09-06-2010, 03:19 PM
  2. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  3. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM
  4. Need help regarding Arrays Help pls....
    By shaggyoo7 in forum New To Java
    Replies: 3
    Last Post: 01-14-2009, 04:36 AM
  5. arrays
    By hasysf in forum New To Java
    Replies: 12
    Last Post: 07-28-2008, 02:38 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •