Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Sorting list code

  1. #1
    Junior Member
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Sorting list code

    My code is bellow and I have attached the text file to run on it but I need it to sort the numbers from highest to lowest


    baseballinput.txt




    package baseball;

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

    public class baseball {

    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[] sortList = new double [40];
    String[] nameList = new String[40];

    Scanner console = new Scanner(System.in);

    //Asks user for input file name
    //If the file is stored on your desktop it will be something like:
    // C:\Users\Nick\baseballinput.txt
    System.out.println("Input File Name: ");
    String inputFileName = console.next();

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

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

    while(in.hasNext())
    {
    for (int i= 0 ; i<= 39; i++)
    {
    lastName = in.next();
    firstName = in.next();
    hits = in.nextInt();
    walks = in.nextInt();
    caughtStealing = in.nextInt();
    stolenBases = in.nextInt();
    atBats = in.nextInt();
    totalBases = in.nextInt();
    runsCreated = ((double)(hits + walks - caughtStealing ) * (totalBases + (.55 * stolenBases ))/(double)(atBats + walks ));

    runsCreatedList[i] = runsCreated;
    sortList[i] = runsCreated;
    nameList[i] = firstName +" "+lastName;
    }
    }

    //sort list here

    for(int j = 0; j <= 39; j++)
    {
    for(int m = 0; m <= 39; m++)
    {

    if(runsCreatedList[m] == sortList[j])
    {
    System.out.print(nameList[m] + ": "+ runsCreatedList[m] + "\n");
    }

    }
    }


    in.close();
    }
    }


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,606
    My Mood
    Hungover
    Thanks
    145
    Thanked 658 Times in 563 Posts

    Default Re: Sorting list code

    Did you have a question, or are you just posting your homework?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Sorting a Linked List
    By nWeid1 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: March 21st, 2012, 08:07 AM
  2. Your input with written code (Sorting and searching within array)
    By GalBenH in forum What's Wrong With My Code?
    Replies: 9
    Last Post: January 11th, 2012, 09:19 AM
  3. Sorting and Searching: Most Efficient Code
    By nicsa in forum What's Wrong With My Code?
    Replies: 7
    Last Post: November 29th, 2011, 11:20 PM
  4. Reading file of floats into array list and sorting into subsets
    By Skave in forum Collections and Generics
    Replies: 2
    Last Post: November 9th, 2011, 07:03 PM
  5. Sorting a doubly linked list
    By snufkin in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 26th, 2010, 12:01 PM