2

I have an assignment due and the last part of the assignment asks:

Suppose s is any string. Write a sequence of statements that will print a random character from s.

Here is what I have come up with so far:

for(int j = 0; j < s.length(); j++){
}
int l = ((int)Math.random()*s.length());
char ch = s.charAt(l);
System.out.println(ch);

I think these are the basic concepts I need to learn how to understand/use to write this code successfully. What I am confused on is where these specific lines of code go, for example if the charAt method should come before the loop, etc.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Nathan
  • 101
  • 1
  • 9
  • 2
    Have you tried your code? Does it do what you want? (Does taking the loop out make any difference?) – khelwood Feb 10 '16 at 22:02
  • You might want to change the `int l` line to `int l = (int) (Math.random()*s.length());`, since the current line will always generate zero. – khelwood Feb 10 '16 at 22:10
  • Thank you all, turns out I didn't need the loop piece in there. Much appreciated. – Nathan Feb 10 '16 at 22:18

4 Answers4

4

You almost had it already. I think your main issue was this part:

int l = ((int)Math.random()*s.length());

Your (int) cast is misplaced. If you read the javadoc of Math.random() you see that it returns a double value "greater than or equal to 0.0 and less than 1.0". Casting values of this range to int (i.e. simply cutting off all decimal places) will always result in 0, which only prints the first character of the string.

The solution is to first multiply it with the string's length and do the cast afterwards:

int l = (int)(Math.random()*s.length());

If you only want to print one random character, you don't need a loop of any sort, so you can delete that from your code.

See this fiddle for a working example. What you still need to do is think about how to get the input string (hint: maybe read it from System.in).

public static void main (String[] args) throws java.lang.Exception
{
    String s = "foobar42";
    int l = (int)(Math.random()*s.length());
    char ch = s.charAt(l);
    System.out.println(ch);
}

And to finally show off in class, you could also have a look at the Random class which could replace the above line with something like

int l = new Random().nextInt(s.length());

and see if you can grasp the difference between those two approaches. Although that is completely irrelevant to your assignment and way out of scope.

Community
  • 1
  • 1
Marvin
  • 13,325
  • 3
  • 51
  • 57
1

You can get a random character by using s.charAt(x), where x is a random number between 0 and the length of the String-1.

The code for this is as follows:

String s = "text string";
Random rand = new Random();
int randomIndex = rand.nextInt(s.length());//returns a random number between 0 and the index of the last character of the string
System.out.println(s.charAt(randomIndex));

When you need to do this several times, you just put it in a loop like this:

String s = "text string";

for(int i = 0; i < 10; i++) { //prints 10 random characters from the String
    Random rand = new Random();
    int randomIndex = rand.nextInt(s.length());//returns a random number between 0 and the index of the last character of the string
    System.out.println(s.charAt(randomIndex));
}
Krasimir Stoev
  • 1,734
  • 11
  • 9
1

Try this approach:

String s = "hello world";
System.out.println(Character.toString(s.charAt((new Random()).nextInt(s.length()))));
  • the s.length() returns the size of s;
  • the (new Random()).nextInt returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive);
  • the s.charAt returns the character at the position specified
  • the Character.toString returns the string representation of the specified character
António Ribeiro
  • 4,129
  • 5
  • 32
  • 49
1

I would do it like this:

System.out.println(s.charAt((int)(Math.random() * s.length())));
daniel.keresztes
  • 875
  • 2
  • 15
  • 25