简体   繁体   English

我需要从文本文件中读取并保存到数组中,然后生成报告

[英]i need to read from a text file save to an array and then generate a report

Create a loop that works till the array of passengers is empty. 创建一个循环,直到乘客排空为止。 Inside this loop you have to: Generate a random number from 1 to 6.Add to the Queue, which you have in your Airport class, a number of passengers equal to the number that you just generated. 在此循环内,您必须:生成1到6之间的随机数。将您的乘客人数等于您刚刚生成的人数添加到您的Airport类中的队列中。 You need to take these passengers from the array. 您需要从阵列中带走这些乘客。 I cant get it to add elements from the array to the queue . 我无法获取将数组中的元素添加到队列中的信息。 Here is what i have tried 这是我尝试过的

  void runningStimulation() throws FileNotFoundException, IOException {

    String Line = "";

    Scanner inFile1 = new Scanner(new BufferedReader(new FileReader("passengers.dat")));
    List<String> temps = new ArrayList<String>();

    while (inFile1.hasNext()) {

        Line = inFile1.nextLine();
        temps.add(Line);

    }
    inFile1.close();
    String[] passengers = temps.toArray(new String[0]);

    for (String s : passengers) {
        System.out.println(s);

        int Random = new Random().nextInt(6);
        int k = Random;
        addqueue(passengers(k));

You should use: 您应该使用:

int k = new Random().nextInt(6) + 1;

because Random().nextInt(n) gives you a random in [0,n[. 因为Random()。nextInt(n)为您提供了[0,n [。

after your comment i would suggest this to replace your for: 在您发表评论后,我建议您将其替换为:

int rng = new Random().nextInt(6) + 1;
for(int i = 0; i < rng; i++){
    Passenger p = new Passenger(passenger[i] /* TODO: default_constructor, assuming that the argument for Passenger constructor is String */);
    addqueue(p);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从文本文件中读取Java中的二维数组 - How do I read from a text file into a 2 dimensional array in Java 如何从文本文件读取数据并将一些数据保存到数组 - How to read data from a text file and save some data from it to an Array 如何从文本文件中读取数字并将其“使用MATLAB或JAVA”保存在数组中? - how to read digits from text file and save it in an array “using MATLAB or JAVA”? 从文本文件中逐行读取并将每一行保存到数组中的不同单元格 - Read line by line from text file and save each line to different cell in array 如何从终端读取文本文件并将输出保存到Java中的另一个文件? - How can I read a text file from the terminal and save the output to another file in java? 将输入从文本文件保存到数组 - save input from text file to array 使用流读取文本文件并保存到 BigInteger 数组 - Using streams to read a text file and save to the BigInteger array 在Java中,我需要读取一个文本文件并将每一行放在单独的数组中。 但是每次阅读文本文件时,我都无法分开 - In Java I need to read a text file and put each line in a separate array. But every time I read the text file I cannot split the lines 需要serversocket逐个字符地将文本文件读取为二维数组 - need serversocket to read a text file into 2d array character by character 如何将文本文件读入数组? - How can I read a text file into an array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM