-4

我有一个文本文件:-

2,5,6,10,5,30


我正在尝试使用此代码来读取文件:-

package str;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.InputStreamReader;

public class getall {

    /**
     * @param args
     */
         public static void main(String args[])
            {
              try{
                // Open the file that is the first 
                // command line parameter
                FileInputStream fstream = new FileInputStream("D://Workspace_J/getstr/src/str/activity.text");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                    // Print the content on the console
                    System.out.println (strLine);
                }
                //Close the input stream
                in.close();
                }catch (Exception e){//Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }
              for(int a =1;a<3;a = a+1)
                {
                    System.out.println("get value from the file randomly");
                }
            }

    }

我可以成功读取文件内容,
但我想使用 for 循环读取随机值......
我该怎么做?

4

3 回答 3

2

String.split(",")您可以使用和使用将值放入数组中Random.nextInt(array.length)

用代码编辑:

public static void main(String args[])
{
    try
    {
        ...
        // assuming your string is "2,5,6,10,5,30"
        String[] tokens = strLine.split(",");
        // tokens will be [2,5,6,10,5,30], size = 6
        Random r = new Random();
        for (int a = 1 ; a < 3 ; a++) //a++ is a short way of writing a = a + 1
        {
            int randomInt = r.nextInt(tokens.length); // an integer between 0 and 5
            System.out.println(tokens[randomInt]);
        }
    }
    ...
}
于 2013-05-27T12:33:42.373 回答
0

使用Math.random

String s = "2,5,6,10,5,30";
String[] a = s.split(",");
double r = Math.random();
int i = (int) (r * a.length);        
System.out.println(a[i]);
于 2013-05-27T12:32:58.030 回答
0

我试试这个...

try{
                // Open the file that is the first 
                // command line parameter
                FileInputStream fstream = new FileInputStream("D://Workspace_J/getstr/src/str/activity.text");
                BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
                String strLine;
                //Read File Line By Line
                while ((strLine = br.readLine()) != null)   {
                    // Print the content on the console
                    System.out.println (strLine);
                }
                String[] tokens = strLine.split(",");
                for (int a = 1 ; a < 3 ; a++) //a++ is a short way of writing a = a + 1
                {
                    System.out.println(tokens[Random.nextInt(tokens.length)]);
                }
                //Close the input stream
                in.close();
                }
              catch (Exception e){//Catch exception if any
                    System.err.println("Error: " + e.getMessage());
                }
于 2013-05-27T12:52:52.610 回答