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!!
shed7Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TolsCommented:
Tell us what You've done already.
shed7Author Commented:
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.
jimmackCommented:
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();
    }
}
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

TolsCommented:
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();
   }
}
shed7Author Commented:
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 ;)
jimmackCommented:
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 ;-) ?
shed7Author Commented:
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!! ;)
jimmackCommented:
Here's my previous post, updated with the mods.  I've broken down the lineOfData creation so that you can change the individual fields more easily and I've included another random number creation line for the phone number.

If you want to create addresses and states randomly, then you should store some in a String array and pick them out at random, like I've done for a few states.

import java.io.*;
import java.util.Random;

public class RandomData
{
    private String[] FIRST_NAMES = {"Thomas", "Edward", "Henry", "Gordon", "James"};
    private String[] SURNAMES = {"Smith", "Jones", "Brown", "Johnson", "Williams"};
    private String[] STATES={"California", "Ohio", "New York", "Texas"};

    private Random random;
   
    private void init()
    {
        random = new Random(1);
        try
        {
            BufferedWriter bw = new BufferedWriter(new FileWriter("datafile.txt"));
            StringBuffer lineOfData;

            int earnings;
            int phoneNumber;

            for (int i = 0; i < FIRST_NAMES.length; i++)
            {
                for (int j = 0; j < SURNAMES.length; j++)
                {
                    earnings = (random.nextInt(5) + 1) * 1000;
                    phoneNumber = 1000000 random.nextInt(1000000);  // gives values 1000000 to 1999999
                    lineOfData = new StringBuffer(FIRST_NAMES[i]);
                    lineOfData.append(",");
                    lineOfData.append(SURNAMES[j]);
                    lineOfData.append("', ");
                    lineOfData.append(earnings);
                    lineOfData.append(", '");
                    lineOfData.append("37745/");
                    lineOfData.append(phoneNumber);
                    lineOfData.append("', '");
                    lineOfData.append("arrowstreet 5");
                    lineOfData.append("', '");
                    lineOfData.append(STATES[random.nextInt(STATES.length)]);
                    lineOfData.append("', NULL);");
                    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();
    }
}

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
shed7Author Commented:
hey thank you very much!!!!!! It works great!!
jimmackCommented:
:-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.