There are at least two ways to generate a random number between 0 and a number (exclusive), one is using a call to Random.nextInt(int) the Javadoc reads in part returns a pseudorandom, uniformly distributed int
value between 0 (inclusive) and the specified value (exclusive) and String.charAt(int) the Javadoc says (in part) returns the char
value at the specified index.
static Random rand = new Random();
public static char selectAChar(String s) {
return s.charAt(rand.nextInt(s.length()));
}
For a second way you might use String.toCharArray() and Math.random() like
public static char selectAChar(String s) {
return s.toCharArray()[(int) (Math.random() * s.length())];
}
And of course, you could use (the somewhat warty) toCharArray()[int]
and charAt(int)
with either method.
Since you are returning a value, your caller should save it
char ch = selectAChar(s);
And then you might format the input String
and print the random char
like
System.out.printf("'%s' %c%n", s, ch);