Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Math.Random method

Status
Not open for further replies.

mutual

Programmer
Jun 8, 2000
6
US
Please give me some idea how to constract sentence.<br><br>I have 4 arrays type String<br><br>&nbsp;public static void main(String args[] ) {<br><br>&nbsp;&nbsp;&nbsp;&nbsp;String[] article = {&quot;the&quot;,&quot;a&quot;,&quot;one&quot;,&quot;some&quot;,&quot;any&quot;};<br>&nbsp;&nbsp;&nbsp;&nbsp;String[] noun = {&quot;boy&quot;,&quot;girl&quot;,&quot;dog&quot;,&quot;town&quot;,&quot;car&quot;};<br>&nbsp;&nbsp;&nbsp;&nbsp;String[] verb = {&quot;drove&quot;,&quot;jumped&quot;,&quot;walked&quot;,&quot;ran&quot;,&quot;skipped&quot;,&quot;hopped&quot;};<br>&nbsp;&nbsp;&nbsp;&nbsp;String[] preposition = {&quot;with&quot;,&quot;from&quot;,&quot;over&quot;,&quot;under&quot;,&quot;on&quot;};<br>&nbsp;&nbsp;I need to construct sentence composed of randomly choosen words from those arrays,capitalize first letter of sentence,place period at the end of sentence and place space character between words.The problem is that all of this have to be in onesingle large string.I have real problem mixing these functions together.Please help!!!
 
String yourString = article[(int)(article.length*Math.random())] + &quot; &quot; + noun[(int)(noun.length*Math.random())] + &quot; &quot; + verb[(int)(verb.length*Math.random())] + &quot; &quot; + preposition[(int)(preposition.length*Math.random())];<br><br>If you have to do it in one line of code, that's it. As for making the first letter capital... I'm not sure if you can use toUpperCase() while you're forming the string- you might need to make your string and then invoke toUpperCase. If this isn't the truth, please let me know, because I have a program that has to convert the first string to lowercase, and I'm using a lot of code to do it- I'm taking substrings and adding them, it looks ugly. You can't do that here without making sure that the random number is the same both times..<br><br>Well, have fun. <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
I tried this and it works, you may have to modify it for your situation:<br><br>public class&nbsp;&nbsp;Test<br>{<br> public static void main(String[] args) <br> {<br> String[] article = {&quot;the&quot;,&quot;a&quot;,&quot;one&quot;,&quot;some&quot;,&quot;any&quot;};<br> String[] noun = {&quot;boy&quot;,&quot;girl&quot;,&quot;dog&quot;,&quot;town&quot;,&quot;car&quot;};<br> String[] verb = {&quot;drove&quot;,&quot;jumped&quot;,&quot;walked&quot;,&quot;ran&quot;,&quot;skipped&quot;,&quot;hopped&quot;};<br> &nbsp;&nbsp;&nbsp;&nbsp;String[] preposition = {&quot;with&quot;,&quot;from&quot;,&quot;over&quot;,&quot;under&quot;,&quot;on&quot;};<br>&nbsp;&nbsp;<br> String randString = article[(int)(Math.random()*(article.length - 1))].toUpperCase() + &quot; &quot; + noun[(int)(Math.random()*(noun.length - 1))] + &quot; &quot; + verb[(int)(Math.random()*(verb.length - 1))] + &quot; &quot; + preposition[(int)(Math.random()*(verb.length - 1))] + &quot;.&quot;;<br> <br> System.out.println(randString);<br><br> }<br>}<br><br><br><br>I hope this helps <p> fenris<br><a href=mailto:fenris@hotmail.com>fenris@hotmail.com</a><br><a href= > </a><br>
 
fenris,<br>first thing is, you should be able to not need the length - 1; you can just use length. Otherwise, the last word will never come up.<br><br>second thing is that toUpperCase() will capitalize the entire string, not just the first character.<br><br><br>And I forgot the period in my example :eek:) <p>Liam Morley<br><a href=mailto:lmorley@wpi.edu>lmorley@wpi.edu</a><br><a href=] :: imotic :: website :: [</a><br>"light the deep, and bring silence to the world.<br>
light the world, and bring depth to the silence.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top