Link to home
Start Free TrialLog in
Avatar of shed7
shed7

asked on

random values stored in a .txt file

hi all together,

I´m a completely newbie in java, but I think my question shouldn´t be too difficult for you :)

I want to fill a (allready existing) sql database with 1000 different values. For that I need a txt file that contains the values. So I need a little programm that generates me those values an stores it in a text file. I thought it could be realized with for example 25 firstnames stored in an array, 50 secondnames stored in an array... other values also stored in arrays, and they should be put together with a random function, so that in the end I´ve got 1000 different values

For example the result shall be as follows:

insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('mike', 'miller', 4000, '37745/1467293', 'arrowstreet 5', 'california', NULL);
insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('frank', 'hanson', 1500, '3345/3543657', 'streetstreet 32', 'queens', 1);
insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('fred', 'hamilton', 2000, '3345/65756762', 'streetstreet 34', 'losangeles', 2);
.
.
.

hope that somebody can help me!! thx for your attention!!
Avatar of Tols
Tols

Tell us what You've done already.
Avatar of shed7

ASKER

sorry I´ve done nothing yet. I just have an sql table which I want to fill with 1000 values. For that I need a txt file in whit those values are stored in like:

insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('mike', 'miller', 4000, '37745/1467293', 'arrowstreet 5', 'california', NULL);
insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('frank', 'hanson', 1500, '3345/3543657', 'streetstreet 32', 'queens', 1);
insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('fred', 'hamilton', 2000, '3345/65756762', 'streetstreet 34', 'losangeles', 2);
.
.
.

I thought, with Java it´s rather easy to realize that.
Here is a start for you.  It combines two first names and surnames from the arrays in sequence.  Doing this randomly could easily result in duplicates.

You'll need to expand it to fill in all the other fields, but that should be easy from this point ;-)

RandomData.java
------------------------
import java.io.*;

public class RandomData
{
    private String[] FIRST_NAMES = {"Thomas", "Edward", "Henry", "Gordon", "James"};
    private String[] SURNAMES = {"Smith", "Jones", "Brown", "Johnson", "Williams"};
   
    private void init()
    {
        try
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter("datafile.txt"));
            StringBuffer lineOfData;
            for (int i = 0; i < FIRST_NAMES.length; i++)
            {
                for (int j = 0; j < SURNAMES.length; j++)
                {
                    lineOfData = new StringBuffer(FIRST_NAMES[i]);
                    lineOfData.append(",");
                    lineOfData.append(SURNAMES[j]);
                    bw.write(lineOfData.toString(), 0, lineOfData.length());
                    bw.newLine();
                }
            }
           
            bw.close();
        }
        catch(IOException ioe)
        {
            System.err.println("IOE: " + ioe.toString());
        }
    }
   
    public static void main(String[] args)
    {
        new RandomData().init();
    }
}
For simple import to MySQL You may generate some csv file - text with recordds in rows and fields separated by for ex. tab. This kind of file could be simple imported by sql query.
Code may look like:

public class Generator
{
    public String getRandomFirstName();
    public String getRandomSecondname();
    public String getRandomEarnings();
    public String getRandomTelephon();
    public String getRandomStreet();
    public String getRandomCity();
    public String getRandomNumber();

    public void generateFile(File file)
   {
        BufferedWriter bw = new BufferedWriter(new FileWriter(file));
        String record = "";
        for (int i=0;i<1000;i++)
        {
              record =
                        getRandomFirstName() + "\t" +
                        getRandomSecondname()  + "\t" +
                        getRandomEarnings() + "\t" +
                        getRandomTelephon()+ "\t" +
                        getRandomStreet()+ "\t" +
                        getRandomCity()+ "\t" +
                        getRandomNumber();
              bw.write(record, 0, record.length());
              bw.newLine()
        }
        bw.close();
   }
}
Avatar of shed7

ASKER

hey thank you guys.
@jimmack: works great, but how does it have to look like, if I want that the output in the txt file is exactly:

insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('mike', 'miller', 4000, '37745/1467293', 'arrowstreet 5', 'california', NULL);

and only the values like 'mike', 'miller', 4000, '37467/394394',... change randomly ? something like:
                    lineofData.append("insert into member (firstname, secondname, earnings, telephon, street, city, number) values ("
                    lineOfData = new StringBuffer(FIRST_NAMES[i]);
                    lineOfData.append(",");
                    lineOfData.append(SURNAMES[j]);
                    bw.write(lineOfData.toString(), 0, lineOfData.length());
                    bw.newLine();

or hmm?? as I said before, I´m a completely newbie in java ;)
Very close ;-)

You need to add the code to generate the earnings, telephon, street, city and number values.  How you do this depends on what your record key is (ie. how you determine uniqueness of a record).  If this is just test data and only the names are important, then you could do the following:

    lineOfData = new StringBuffer("insert into member (firstname, secondname, earnings, telephon, street, city, number) values ('";
    lineOfData.append(FIRST_NAMES[i]);
    lineOfData.append("', '");
    lineOfData.append(SURNAMES[i]);
    lineOfData.append("', 4000, '37745/1467239', 'arrowstreet 5', 'california', NULL);");
    bw.write(lineOfData.toString(), 0, lineOfData.length());
    bw.newLine();

A couple of things to note:

1) Be careful about making sure that the single apostrophe is included in the string in the correct place
2) If you don't want to have the same data (other than name) for every line, you'll need to figure out what you want instead.  eg. You could use a random number generator to put in random earnings and telephone numbers.

import java.util.Random;
.
.
private Random random;
.
.
random = new Random();  // If you want the same random sequence every time, use random = new Random(1);
.
.
int earnings = (random.nextInt(5) + 1) * 1000;  // Gives a value of 1000 to 5000

Then you can include the earnings value in the string:

    lineOfData.append(SURNAMES[i]);
    lineOfData.append("', ");
    lineOfData.append(earnings);
    lineOfData.append(", '37745/1467239', 'arrowstreet 5', 'california', NULL);");

Any good ;-) ?
Avatar of shed7

ASKER

Yes, thats much better!! Although its just a test data I actually want every value as a random value. I´m not shure where I have to place
private Random random;
.
.
random = new Random();  // If you want the same random sequence every time, use random = new Random(1);
.
.
int earnings = (random.nextInt(5) + 1) * 1000;  // Gives a value of 1000 to 5000


and how can I use that random function for the other values telephon, street, city, number ??

oh yes ít´s already very close and I can see the end of the tunnel!! ;)
ASKER CERTIFIED SOLUTION
Avatar of jimmack
jimmack

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shed7

ASKER

hey thank you very much!!!!!! It works great!!
:-)