如何对从JAVA中的.txt文件中读取的数组进行排序

时间:2014-04-07 13:59:16

标签: java arrays eclipse sorting

这是我的代码

import java.io.File;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.Scanner;
import java.util.Arrays;

public class runsCreated {

    public static void main(String[] args) throws FileNotFoundException {
        String   firstName       = "";
        String   lastName        = "";
        int      hits            = 0;
        int      walks           = 0;
        int      caughtStealing  = 0;
        int      stolenBases     = 0;
        int      atBats          = 0;
        int      totalBases      = 0;
        double   runsCreated     = 0.0;
        double[] runsCreatedList = new double[40];
        double[] sortedList      = new double[40];
        String[] nameList        = new String[40];      
        Scanner  console         = new Scanner(System.in);

        //Asks user for input file name
        System.out.println("Input File Name:");
        String inputFileName = "//Users//Mitchell//Desktop//baseballinput.txt"; //console.next();


        //inputFile will take input from stored input file
        File inputFile = new File(inputFileName);

        //in will take input from user defined source 
        Scanner inFile = new Scanner(inputFile);

        int i = 0; //counter

        while(inFile.hasNext()) {
            lastName           = inFile.next();
            firstName          = inFile.next();
            hits               = inFile.nextInt();
            walks              = inFile.nextInt();
            totalBases         = inFile.nextInt();
            stolenBases        = inFile.nextInt();
            caughtStealing     = inFile.nextInt();
            atBats             = inFile.nextInt();

            runsCreated        = ((hits + walks - caughtStealing) * (totalBases + (.55 * stolenBases)) / (atBats + walks));
            runsCreatedList[i] = runsCreated;       
            sortedList[i] = runsCreatedList[i];

            System.out.print(nameList[i] + ":" + " " + /*will be sortedList[i]*/runsCreatedList[i]);

            i++;
        }

        sortedList[i] = Arrays.sort(sortedList);        
        inFile.close();     
    }
}

我在sortedList[i] = Arrays.sort(sortedList);上收到错误消息,指出它无法从void转换为double。我知道它应该是double我无法弄清楚如何修复它。任何帮助,将不胜感激。

1 个答案:

答案 0 :(得分:3)

Arrays.sort(array);只是对数组进行排序。它没有任何回报。 Documentation。如果你期待双倍的话,你想做什么?你能举个例子吗?