aspose file tools*
The moose likes Beginning Java and the fly likes generate a text file with random no of characters Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "generate a text file with random no of characters" Watch "generate a text file with random no of characters" New topic
Author

generate a text file with random no of characters

mark stone
Ranch Hand

Joined: Dec 18, 2001
Posts: 417
how to generate a text file that contains a random string of letters or characters ?
ie it should be able to control the number of letters in one line and the number of lines in the text file.
please email me a cc at markstone23@yahoo.com
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Very funny.
What have you tried so far?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
You can generate random characters using the Math.random() method.
With it you get a random [0-1] number.You can multiply it by n(the number of letters you have),round it(the method returns a double), and you have an random 0-n number.
Just assing a numeric value for each letter.
-HTH
Juanjo Bazan
Ranch Hand

Joined: Feb 04, 2002
Posts: 231
with this method you can generate "words" with the lenght you want:
//the argument n is the number of words you want
//the argument len is the lenght of the words
public String[] generateWords(int n, int len){
String[] words=new String[n];
char[] eachWord=new char[len];
for (int j=0;j<n;j++){
for (int i=0;i<len;i++){
//I assume you have a method convertToLetter where you have assigned a numeric value to each letter, and which receives a 0-1 number and returns a char.
eachWord[i]=convertToLetter(Math.Random());
}
words[j]=new String(eachWord);
}
return words;
}//end of method
-HTH
[ July 12, 2002: Message edited by: Juanjo Bazan ]
[ July 12, 2002: Message edited by: Juanjo Bazan ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generate a text file with random no of characters