Click here to Skip to main content
Rate this: bad
good
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 8:42am
Edited 18-Nov-12 8:46am
ridoy52K
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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 at 18-Nov-12 15:40pm
   
it cant be passed as a parameter though since the method takes no parameters
Christian Graus at 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 at 18-Nov-12 16:45pm
   
Why can't you modify method signature?
diego14567 at 18-Nov-12 16:19pm
   
how would i go about doing that
diego14567 at 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
0 OriginalGriff 520
1 Maciej Los 290
2 Richard MacCutchan 265
3 BillWoodruff 235
4 Suraj Sahoo | Coding Passion 155
0 OriginalGriff 8,764
1 Sergey Alexandrovich Kryukov 7,437
2 DamithSL 5,639
3 Maciej Los 5,279
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web03 | 2.8.1411028.1 | Last Updated 18 Nov 2012
Copyright © CodeProject, 1999-2014
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