user2410266
user2410266

Reputation: 551

How to select random text value from specific row using java

I have three input fields.

I would like to get random data for each input from a property file. This is how the property file looks. Field name and = should be ignored.

 - First Name= Robert, Brian, Shawn, Bay, John, Paul
 - Last Name= Jerry, Adam ,Lu , Eric
 - Date of Birth= 01/12/12,12/10/12,1/2/17

Example: For First Name: File should randomly select one name from the following names

  Robert, Brian, Shawn, Bay, John, Paul

Also I need to ignore anything before =

FileInputStream objfile = new FileInputStream(System.getProperty("user.dir "+path);
in = new BufferedReader(new InputStreamReader(objfile ));
String line = in.readLine();

    while (line != null && !line.trim().isEmpty()) {
        String eachRecord[]=line.trim().split(",");
        Random rand = new Random();
//I need to pick first name randomly from the file from row 1.                        
        send(firstName,(eachRecord[0]));

Upvotes: 0

Views: 287

Answers (2)

so cal cheesehead
so cal cheesehead

Reputation: 2573

If you know that you're always going to have just those 3 lines in your property file I would get put each into a map with an index as the key then randomly generate a key in the range of the map.

// your code here to read the file in

HashMap<String, String> firstNameMap = new HashMap<String, String>();
HashMap<String, String> lastNameMap = new HashMap<String, String>();
HashMap<String, String> dobMap = new HashMap<String, String>();

String line;
while (line = in.readLine() != null) {
    String[] parts = line.split("=");
    if(parts[0].equals("First Name")) {
        String[] values = lineParts[1].split(",");
        for (int i = 0; i < values.length; ++i) {
            firstNameMap.put(i, values[i]);
        }
    }
    else if(parts[0].equals("Last Name")) {
        // do the same as FN but for lastnamemap
    }
    else if(parts[0].equals("Date of Birth") {
       // do the same as FN but for dobmap
    }
}

// Now you can use the length of the map and a random number to get a value
// first name for instance:
int randomNum = ThreadLocalRandom.current().nextInt(0, firstNameMap.size(0 + 1);
System.out.println("First Name: " + firstNameMap.get(randomNum));

// and you would do the same for the other fields

The code can easily be refactored with some helper methods to make it cleaner, we'll leave that as a HW assignment :)

This way you have a cache of all your values that you can call at anytime and get a random value. I realize this isn't the most optimum solution having nested loops and 3 different maps but if your input file only contains 3 lines and you're not expecting to have millions of inputs it should be just fine.

Upvotes: 1

Roy Spector
Roy Spector

Reputation: 151

Haven't programmed stuff like this in a long time. Feel free to test it, and let me know if it works.

The result of this code should be a HashMap object called values

You can then get the specific fields you want from it, using get(field_name)

For example - values.get("First Name"). Make sure to use to correct case, because "first name" won't work.

If you want it all to be lower case, you can just add .toLowerCase() at the end of the line that puts the field and value into the HashMap

import java.lang.Math;
import java.util.HashMap;

public class Test
{
    // arguments are passed using the text field below this editor
    public static void main(String[] args)
    {

        // set the value of "in" here, so you actually read from it

        HashMap<String, String> values = new HashMap<String, String>();
        String line;
        while (((line = in.readLine()) != null) && !line.trim().isEmpty()) {
            if(!line.contains("=")) {
                continue;
            }
            String[] lineParts = line.split("=");
            String[] eachRecord = lineParts[1].split(",");
            System.out.println("adding value of field type = " + lineParts[0].trim());

            // now add the mapping to the values HashMap - values[field_name] = random_field_value
            values.put(lineParts[0].trim(), eachRecord[(int) (Math.random() * eachRecord.length)].trim());
        }
        System.out.println("First Name = " + values.get("First Name"));
        System.out.println("Last Name = " + values.get("Last Name"));
        System.out.println("Date of Birth = " + values.get("Date of Birth"));
    }
}

Upvotes: 1

Related Questions