There is a text file in which several lists are stored to achieve random access to a person's name

Source: Internet
Author: User

Package Cn.idcast2;import Java.io.bufferedreader;import Java.io.filenotfoundexception;import java.io.FileReader; Import java.io.ioexception;import java.util.arraylist;import java.util.random;/* Analysis: * A, the data stored in the text file in the collection * B, randomly generate an index * C, get a value based on the index *  */public class Day26 {public static void main (string[] args) throws IOException {BufferedReader br = NE W BufferedReader (New FileReader ("A.txt")); arraylist<string> array = new arraylist<string> (); String line = null;while (line = Br.readline ())! = null) {Array.add (line);} Br.close ();//randomly generates an index of random r = new random (), int index = R.nextint (Array.size ());//Gets a value based on the index string name = Array.get ( index); System.out.println ("Lucky person is:" + name);}}

  

There is a text file in which several lists are stored, enabling random access to a person's name

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.