如何使用java从特定行中选择随机文本值

如何使用java从特定行中选择随机文本值,java,selenium,Java,Selenium,我有三个输入字段 名字 最后一项 出生日期 我想从属性文件中获取每个输入的随机数据。 这就是属性文件的外观。应忽略字段名和= - 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 例如:对于First Name:文件应从以下名称中随机选择一个名称 Robert, Brian,

我有三个输入字段

  • 名字
  • 最后一项
  • 出生日期
我想从属性文件中获取每个输入的随机数据。 这就是属性文件的外观。应忽略字段名和=

 - 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
例如:对于First Name:文件应从以下名称中随机选择一个名称

  Robert, Brian, Shawn, Bay, John, Paul
我也需要忽略任何事情之前=

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]));

很久没编过这样的程序了。 请随时测试它,并让我知道它是否有效

这段代码的结果应该是一个名为values的HashMap对象

然后,您可以使用get(field\u name)

例如-values.get(“名字”)。确保使用正确的大小写,因为“名字”不起作用

如果希望所有内容都是小写,只需在将字段和值放入HashMap的行末尾添加.toLowerCase()

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"));
    }
}
import java.lang.Math;
导入java.util.HashMap;
公开课考试
{
//使用此编辑器下面的文本字段传递参数
公共静态void main(字符串[]args)
{
//在这里设置“in”的值,以便实际读取
HashMap值=新的HashMap();
弦线;
而(((line=in.readLine())!=null)和(&!line.trim().isEmpty()){
如果(!line.contains(“=”){
继续;
}
字符串[]lineParts=line.split(“”);
字符串[]eachRecord=lineParts[1]。拆分(“,”;
System.out.println(“添加字段类型的值=“+lineParts[0].trim());
//现在将映射添加到值HashMap-values[field\u name]=random\u field\u value
value.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(“出生日期=“+values.get”(“出生日期”);
}
}

很久没有编写过这样的程序了。 请随时测试它,并让我知道它是否有效

这段代码的结果应该是一个名为values的HashMap对象

然后,您可以使用get(field\u name)

例如-values.get(“名字”)。确保使用正确的大小写,因为“名字”不起作用

如果希望所有内容都是小写,只需在将字段和值放入HashMap的行末尾添加.toLowerCase()

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"));
    }
}
import java.lang.Math;
导入java.util.HashMap;
公开课考试
{
//使用此编辑器下面的文本字段传递参数
公共静态void main(字符串[]args)
{
//在这里设置“in”的值,以便实际读取
HashMap值=新的HashMap();
弦线;
而(((line=in.readLine())!=null)和(&!line.trim().isEmpty()){
如果(!line.contains(“=”){
继续;
}
字符串[]lineParts=line.split(“”);
字符串[]eachRecord=lineParts[1]。拆分(“,”;
System.out.println(“添加字段类型的值=“+lineParts[0].trim());
//现在将映射添加到值HashMap-values[field\u name]=random\u field\u value
value.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(“出生日期=“+values.get”(“出生日期”);
}
}

如果您知道您的属性文件中总是只有这3行,我会将每一行放入一个以索引为键的映射中,然后在映射范围内随机生成一个键

// 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
//在此处读取文件的代码
HashMap firstNameMap=新HashMap();
HashMap lastNameMap=新HashMap();
HashMap dobMap=新的HashMap();
弦线;
while(line=in.readLine()!=null){
String[]parts=line.split(“”);
如果(部分[0]。等于(“名字”)){
字符串[]值=lineParts[1]。拆分(“,”;
对于(int i=0;i
代码可以很容易地用一些助手方法进行重构,以使其更干净,我们将其作为硬件分配:)


通过这种方式,您可以缓存所有值,随时调用这些值并获取随机值。我意识到这并不是具有嵌套循环和3个不同映射的最佳解决方案,但如果您的输入文件仅包含3行,并且您不希望有数百万个输入,那么应该就可以了。

如果您知道自己始终是g如果在属性文件中只包含这3行,我会将每一行放入一个以索引为键的映射中,然后在映射范围内随机生成一个键

// 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
//在此处读取文件的代码
HashMap firstNameMap=新HashMap();
HashMap lastNameMap=新HashMap();
HashMap dobMap=新的HashMap();
弦线;
while(line=in.readLine()!=null){
String[]parts=line.split(“”);
如果(部分[0]。等于(“名字”)){
字符串[]值=lineParts[1]。拆分(“,”;
for(int)