Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
im creating a method that counts the occurances of random characters in another method but i cant seem to figure out how to bring in the random characters in from the other method the code is supposed to create an array of 26 integers then count and store the occurances of the random characters this is my code up until now:
Java
      // generates random characters
public void add (char fromChar, char toChar){      
int range = toChar - fromChar;
int seed = fromChar * toChar;
Random rr = new Random(seed);
for (int i = 0; i < charArray; ++i) {
    char randomChar = (char)(fromChar + rr.nextInt(range));
    System.out.println("Next character: " + randomChar);
}   
 
public int[] countLetters(){ //method im working on
        int [] countLetters =new int[26];
       char current;
       
for(int i=0; i<countletters.length;>   current=.charAt(i);
    if (current >= 'a' && current <= 'z'){
       countLetters[current-'a']++;
   }
}
        return null;   
    }
Posted
Updated 18-Nov-12 7:46am
v2
Comments
Sergey Alexandrovich Kryukov 18-Nov-12 13:50pm    
Why doing all that? And how would you define it: when a character occurs in a method, what is it?
--SA
diego14567 18-Nov-12 14:04pm    
first method:
this method generates random characters between the specified character "fromChar" to specified character "toChar" inclusive. For example if we call add ('a', 'z'), it will create lowercase letters randomly and fill out the array of "charArray" with lowercase letters randomly.
second method:
this method returns an array of twenty-six int values, each of which stores the number of occurrence of each letter contained in the "charArray". For example the first element in the array stores the number of occurrences for letter 'a'.

1 solution

You make the array you want to search a member variable or, better yet, pass it in as a parameter.
 
Share this answer
 
Comments
diego14567 18-Nov-12 15:40pm    
it cant be passed as a parameter though since the method takes no parameters
Christian Graus 18-Nov-12 15:43pm    
I can see that, you didn't say you were not allowed to write sensible code to do this. In that case, it clearly needs to be member variable, so it's visible from all functions.
CPallini 18-Nov-12 16:45pm    
Why can't you modify method signature?
diego14567 18-Nov-12 16:19pm    
how would i go about doing that
diego14567 18-Nov-12 17:53pm    
we are given the main method that cant be modified

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900