2017-09-14 35 views
0

我有三个输入字段。如何使用java从特定行中选择随机文本值

  • 最后一项
  • 出生日期

我想获得的随机数据进行属性文件中的每个输入。 这是属性文件的外观。字段名称和=应该被忽略。

- 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 

示例:名字:文件应随机从下列名称中选择一个名称

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

你是什么意思“它不从特定行中选择文件”?你的意思是“它不从文件中选择特定的行”?或者其他一些事情?在这两种情况下,我不知道你的意思... –

+0

对不起,我的意思是来自特定行的值,如名称 – user2410266

回答

1

有没有编程的东西的事情之前像这样很长一段时间。 随时测试它,并让我知道它是否工作。

这段代码的结果应该是叫

一个HashMap对象,然后你可以让你从它想要的特定领域,使用GET(FIELD_NAME)

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

如果你想这一切要低的情况下,你可以添加.toLowerCase()在那放字段和值到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")); 
    } 
} 
+0

Hi Roy,感谢您的回复。对不起,我还在学习阶段。可以请给我建议我应该如何把Values.get(“名字”)。目前我无法获得价值。它是空的。 – user2410266

+0

您希望用户输入一个字段名称,然后打印适合该字段名称的随机值?我不确定这个程序的行为。它的投入是什么,他们是如何得到的。它的输出是什么,它们是如何呈现的? –

+0

如果我只能从特定行打印随机值,那也可以。 – user2410266

1

行结束时,如果你知道你总是会在你的属性文件中有这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 

代码可以很容易与一些辅助方法进行重构,使其更清洁,我们会离开,作为一个硬件分配:)

这样,你把所有的值的缓存,你可以随时致电并获得随机价值。我意识到这不是具有嵌套循环和3种不同映射的最佳解决方案,但如果您的输入文件只包含3行,并且您不希望拥有数百万个输入,它应该会很好。

+0

谢谢你的解决方案。 – user2410266

+0

@ user2410266希望它有帮助! –