Thursday, April 17, 2014

String problems seek help


String str = ". ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 @ + - * / _";

As the above string, I think inside eight randomly selected to form a new string, how?

Passing under the guidance of the great God.
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
Here did not consider repeated
 public void testString () 
{
Random random = new Random ();
String str = ". ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 @ + - * / _";

int str_len = str.length ();
StringBuffer stringBuffer = new StringBuffer ();
for (int i = 0; i <8; i + +)
{
int temp_radom = random.nextInt (str_len-1)% (str_len);
stringBuffer.append (str.substring (temp_radom, temp_radom +1));
}
System.out.println (stringBuffer.toString ());
}

No comments:

Post a Comment