I\'m having problem writing just what it really wants me to do public void loadC
ID: 3619615 • Letter: I
Question
I'm having problem writing just what it really wants me to dopublic void loadChars(){
/*
* 35 pts
* Use a loop to put all 26 lowercase letters inside our ArrayList.
* Hint. Create a String with all the letters and then put each
* letter into the ArrayList one at a time.
*/
}
---------------------------------------------------------------------------------------
For this one I wrote:
public void loadChars(){
String a = "abcdefghijklmnopqrstuvwxyz";
StringTokenizer tokens = new StringTokenizer(a);
while(tokens.hasMoreTokens()){
list.add((Character) tokens.nextElement());
}
I'm totally sure it's doing what it's asking
_______________________________________________________________
public char randomChar(){
/* 30 pts
* Instantiate a Random object and use it to select a random char from
* our ArrayList. Your Random object can either be an instance field or
* a local variable to this method.
*
* Do not use the get method, use the remove method. We
* want the size of the ArrayList to decrease.
* Use the nextInt method in the Random class.
* You must ask for the size of the ArrayList to do this correctly.
* You don't want to randomly choose the int 24 if the ArrayList doesn't
* contain that many items.
*/
I'm clueless on this one....
Explanation / Answer
Here's one way you can do it: Assume you have an ArrayList defined outside. Something like ArrayList alist = new ArrayList(); Then your method would go something like public void loadChars() { String letters = "abcdefghijklmnopqrstuvwxyz"; while(!letters.equals("")) { alist.add(letters.substring(0, 1); letters = letters.substring(1); } } Here, using a for loop: public void loadChars() { String letters = "abcdefghijklmnopqrstuvwxyz"; for (int k=0; kRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.