Requirements: I have several names stored in a text file. Please write a program to obtain a random name in java.

package cn.itcast_02;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

/*
 *需求: 我有一个文本文文件中存储了几个名称,请大家写一个程序实现随机获取一个名字
 * 
 * 分析:
 *      A:把文本文件中的数据存储到集合中
 *      B:随机产生一个索引
 *      C:根据该索引获取一个值
 * 
 */
public class 随机获取文本文件中的名字 {
    
    

	public static void main(String[] args) throws IOException {
    
    
		// 把文本文文件中的名字添加到集合中
		BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        ArrayList<String> array = new ArrayList<String>();
        String line = null;
        while((line = br.readLine()) != null) {
    
    
        	array.add(line);
        }
        br.close();
        
        //随机产生索引
        Random r = new Random();
        int index = r.nextInt(array.size());
        
        //输出
        String name = array.get(index);
        System.out.println(name);
	}
}

Guess you like

Origin blog.csdn.net/kaszxc/article/details/108653291