简体   繁体   中英

How to sort an Array that is read from a .txt file in JAVA

Here is my code

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();     
    }
}

I am getting an error on the sortedList[i] = Arrays.sort(sortedList); that it cannot convert from void to double . I know it is suppose to be a double I just can't figure out how to fix it. Any help would be appreciated.

Arrays.sort(array); just sorts the array. It doesn't return anything. Documentation . What are you trying to do, if you're expecting a double? Could you give an example?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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