简体   繁体   English

从我用随机整数创建的文件中读取并放置到数组中并显示值

[英]read from a file i created with random ints and place into an array and display values

a. 一种。 Reopen the file for reading. 重新打开文件以进行读取。 b. b。 Read each value from the file and place into an array. 从文件中读取每个值,然后放入数组中。 c. C。 Display each value AFTER it is placed into the array (ie access value from array to display). 在将每个值放入数组后显示它们(即,从数组访问显示值)。

d. d。 Find and display the largest two distinct numbers in the array. 查找并显示数组中最大的两个不同数字。 a. 一种。 It is possible for the array to contain duplicates which causes 1st largest = 2nd largest. 数组可能包含导致第一大=第二大的重复项。 b. b。 Skip duplicates for 2nd largest! 跳过第二大重复! See tips section for details. 有关详细信息,请参见提示部分。 e. e。 Sort the array into descending order (largest to smallest). 将数组按降序排序(从大到小)。 When performing this sort: a. 执行这种排序时: You can use the sort method in the Arrays class b. 您可以在Arrays类b中使用sort方法。 You cannot use the reverseOrder method on the Collections class c. 您不能在Collections类c上使用reverseOrder方法。 You must manually sort the values into descending order. 您必须手动将值按降序排序。 d. d。 Manually sorting means you must write the code that does the sorting without using any methods other than Arrays.sort() f. 手动排序意味着您必须编写用于执行排序的代码,而不使用Arrays.sort()f之外的任何方法。 Display the updated array after manually sorting into descending order. 手动按降序排列后显示更新后的数组。

I have tried using different methods even though it is not necessary for this problem. 尽管没有必要解决此问题,但我尝试使用其他方法。 When I try and compile all I get for my array is [] and no values. 当我尝试编译时,我得到的数组是[]且没有值。

    int max = 100;
    int min = 1;
    Random randomNum = new Random();
    File fileName = new File("assignment1.txt");
    PrintWriter resultsFile = new PrintWriter (fileName);
    System.out.println("Generating random values and writing to a file: ");

    for(int i = 1; i < 26; i++) {
        int showMe = min + randomNum.nextInt(max);

        System.out.println("Writing to a file: ");
        System.out.println(showMe);



        resultsFile.println(showMe);

    }
    resultsFile.close();
    System.out.println("Reading values from file and placing into an array");
    int[] data = readFiles("assignmnent1.txt");
    System.out.println(Arrays.toString(data));
}

public static int[] readFiles(String fileName) {

    try {

        //File f = new File(fileName);
        Scanner s = new Scanner (fileName);
        int ctr = 25;

        while (s.hasNextInt()) {
            ctr++;
            s.nextInt();
        }
        int [] arr = new int[ctr];



        return arr;

    }
    catch (Exception e) {
        return null;
    }

I am not sure where I am going wrong and why it will not display the values in the array. 我不确定哪里出错了,为什么它不会在数组中显示值。

in your readFiles function, you initialized the array but you did not fill it with any values. 在readFiles函数中,您初始化了数组,但没有用任何值填充它。

        int [] arr = new int[ctr];



        return arr;

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM