简体   繁体   中英

How to print a random character from a string?

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.

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 (ie 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.

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));
}

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

我会这样做:

System.out.println(s.charAt((int)(Math.random() * s.length())));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM