4

I'm having problems with an exercise. I need to print five random words, between min and max letters.

This is what I've done:

package Vjezba;
import acm.program.*;
import acm.util.RandomGenerator;
import java.util.*;
public class String2 extends ConsoleProgram {
public void run () {
 for (int i = 0; i<5; i++){
 String a = randomWord();
 println(a);
 }
}
private String randomWord() {
 int a = rgen.nextInt(MIN_LETTER, MAX_LETTER);
 for (int x=0; x<a; x++){
 String niz = "";
 char c = randomChar();
 niz += 'c';
 }
 return niz;
}
private char randomChar(){
 return (char) rgen.nextInt('a', 'z');
}
private static RandomGenerator rgen = new RandomGenerator();
private static int MIN_LETTER = 3;
private static int MAX_LETTER = 10;
 }

I have problems with returning String. Dunno how to do it.

Bill the Lizard
407k213 gold badges577 silver badges892 bronze badges
asked Sep 6, 2011 at 8:10
2
  • 1
    What do you mean when you say you "have problems"? Are you getting an exception? Is nothing being returned? Commented Sep 6, 2011 at 8:13
  • +1 for asking help after you are stuck, and not asking us to do your homework for you. Commented Sep 6, 2011 at 8:54

3 Answers 3

5

You're declaring your String inside your for loop; every time it loops you get a new (empty) String. You're also adding the character 'c', not the contents of your char c

String niz = "";
for (int x=0; x<a; x++){
 //String niz = "";
 char c = randomChar();
 niz += c; // c not 'c'
}

And while in this trivial case it doesn't really matter, a String in java is immutable - it can't be changed. Every time you do niz += c it creates a new string. Any time you're building a string you want to use a StringBuilder:

StringBuilder niz = new StringBuilder();
for (int x=0; x<a; x++){
 char c = randomChar();
 niz.append(c);
}
return niz.toString();
answered Sep 6, 2011 at 8:15
Sign up to request clarification or add additional context in comments.

Comments

3

Ignore my comment, I hadn't woken up yet. Your randomWord() has a scope issue; you're declaring your String variable inside of your for loop and then trying to return it after the loop ends - I imagine you're getting a compile error. Modify it so that the empty String declaration is before the for loop.

answered Sep 6, 2011 at 8:18

Comments

3

niz += 'c'; should be niz = niz + c; or niz += c; [personally I prefer the first, since it is more clear that the object is not modified but the reference is changed].

also, niz should be declared out of the loop's scope [before the for line].

String niz = "";
for (int x=0; x<a; x++){
 char c = randomChar();
 niz = niz + c;
}
return niz;

you might want to use StringBuilder if performance is an issue, but I don't think it is the case in here.

answered Sep 6, 2011 at 8:14

6 Comments

+= works just fine (though, not really as it creates a new String every time the same as niz = niz + c;)
@Brian Roach: Read my answer, I said I prefer it for clarity, not that it doesn't work, the "doesn't work" part refers to 'adding' 'c' instead of c.
Yes, now that you've edited it it does say that. And it notes the problem with the String being declared in the loop and the StringBuilder from my answer ...
@Brian Roach: my answer was editted 8 mins ago, yours 2 mins ago. your comment is 6 mins ago, stop trolling.
Care to look at the edit and see the only thing I added was // c not 'c' or do you want to keep playing this game?
|

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.