Click here to Skip to main content
12,449,149 members (57,091 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: Java
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:
      // 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 18-Nov-12 7:42am
Updated 18-Nov-12 7:46am
ridoy58.6K
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

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You make the array you want to search a member variable or, better yet, pass it in as a parameter.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160811.3 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100