-1

I want to add sample test data to my NoteTableModel.

Currently I have it set up like this:

public void buildTestNoteTable(){

    String uuid = UUID.randomUUID().toString().substring(0,10);
    //System.out.println(uuid);


    for (int i = 0; i < 2000; i++){
        EssayNote newNote = new EssayNote(i, 20131105, "New Test note" +i, uuid);
        noteTableData.add(newNote); 
    }  
}

What I want to do is to have it so that the section that says "Blah Blah Blah" is randomly generated letters with a length of 10. How should I update my current method to be able to do this ?

EDIT: I updated the code using UUID. I replaced the hard coded "Blah Blah Blah" with the uuid and I am now getting a random serial number but I am getting the same number for every instance of note, which I do not want. How can I make it so that every EssayNote has a different UUID ?

Erv Noel
  • 85
  • 1
  • 3
  • 10
  • see this question: http://stackoverflow.com/questions/2626835/is-there-functionality-to-generate-a-random-character-in-java – akostadinov Dec 12 '13 at 08:18
  • You should be using property based testing (quickcheck). –  Dec 12 '13 at 08:20
  • What properties must the random text have? Must it be split into words? Must they be words of a particular language? – Raedwald Dec 12 '13 at 08:26

3 Answers3

2

Use Apache commons RandomStringUtils.random(10) : https://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/RandomStringUtils.html

FiNaLsPY
  • 55
  • 4
0

You can use UUID. In order to use it you should import

import java.util.UUID;

Then in your code you can call

String uuid = UUID.randomUUID().toString().substring(0,10);
System.out.println("uuid = " + uuid);

Hope this code will help you

OR You can use custom method

String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int length = 10;
public String generateString(){
    char[] randText = new char[length];
    for (int i = 0; i < length; i++) {
        randText[i] = characters.charAt(Math.random()*length);
    }
    return new String(randText);
}

I haven't compiled this piece of code, but it should work properly

abekenza
  • 1,090
  • 2
  • 11
  • 19
  • I used the first strategy with the UUID and it generated a random string but it's the same string for every single Note that I have. I updated my code above so you can see what I am working with. What am I doing wrong ? – Erv Noel Dec 12 '13 at 20:30
  • You should put : String uuid = UUID.randomUUID().toString().substring(0,10); inside of a for loop – abekenza Dec 13 '13 at 04:16
0

You can also generate some numbers with the Java Random function, between 64-91, and treat them as ASCII characters. Wich will give you random characters

Black Magic
  • 2,706
  • 5
  • 35
  • 58