Results 1 to 8 of 8
  1. #1
    ddc
    ddc is offline Member
    Join Date
    Dec 2010
    Posts
    1
    Rep Power
    0

    Post Trying to sort Array<int, String>

    I'm new to the forum and am trying to sort a custom Class Object array in descending order. First, lexicographically based on the String. Second, numerically based on the integer. I am having trouble getting it to sort lexicographically and haven't gotten around to sorting it numerically yet because I am having problems getting the program to output properly. I believe the error lies around line 84. I also copied and pasted the code from the separate class file that holds the Player object and placed it below the main program code. Any help would be greatly appreciated!


    import java.io.*;
    import java.util.*;


    public class ProjectFive
    {
    public static void main(String[] args) throws IOException
    {
    final int NUM_PLAYERS = 6;
    String string1 = "Player", string2 = "Jersey Number";


    //TODO Initialize array of objects
    Player[] p = new Player[NUM_PLAYERS];


    File file1 = new File("Team.txt");
    Scanner inFile = new Scanner(file1);


    //TODO Read data from file into array
    for(int i = 0; i < p.length; i++)
    {
    p[i] = new Player(inFile.nextInt(), inFile.nextLine().trim());
    }

    inFile.close();

    System.out.printf("%10s%20s\n", string1, string2);


    //TODO List data in array (unsorted)
    for(int i = 0; i < p.length; i++)
    {
    System.out.printf("%10s", p[i].getPlayerName());
    System.out.printf("%20d\n", p[i].getPlayerNum());
    }


    //TODO Sort data alphabetically
    int startScan, index, minIndex;
    // minValue temporarily holds the smallest
    // element in the array - needed for swap below
    // minValue will always have the same type as
    // each member of the array
    String minValue;

    // The first time through, look at the array starting at
    // the beginning. Swap the smallest value with the
    // starting value.
    // Each time after that, look at the array starting at
    // the next index, because you know that the smallest
    // value(s) is/are already sorted at the beginning
    // of the array.
    for (startScan = 0; startScan < (p.length-1); startScan++)
    {

    // At the start, the minimum value is just
    // the first value in the array.
    minValue = p[startScan].getPlayerName();
    // For now, the minIndex is the index that we're
    // starting at.
    minIndex = startScan;


    for(index = startScan + 1; index < p.length; index++)
    {
    // Compare and find the smallest value
    if (p[index].getPlayerName().compareTo(minValue) < 0)
    {
    // The array element with the minimum value
    // is saved in the minValue variable
    minValue = p[index].getPlayerName();
    // Remember where the minimum value is
    minIndex = index;
    }
    }
    // Put the value from the front of the array
    // into the place where the minimum value is
    //p[minIndex] = new Player(p[startScan].getPlayerNum(), p[startScan].getPlayerName());
    p[minIndex] = p[startScan];
    // Put the minimum value at the front
    // of the array
    p[startScan] = new Player(p[minIndex].getPlayerNum(), minValue);
    }

    System.out.println();


    System.out.printf("%10s%20s\n", string1, string2);


    //TODO List data in array (now sorted alphabetically)
    for(int i = 0; i < p.length; i++)
    {
    System.out.printf("%10s", p[i].getPlayerName());
    System.out.printf("%20d\n", p[i].getPlayerNum());
    }

    //TODO Sort data numerically


    //TODO List data in array (now sorted numerically)


    }
    }




    public class Player
    {
    //Private data fields
    private int playerNum;
    private String playerName;

    //Constructor. This is the only place to write to data fields
    public Player(int a, String b)
    {
    playerNum = a;
    playerName = b;
    }

    //Accessor for Player Number
    public int getPlayerNum()
    {
    return playerNum;
    }

    //Accessor for Player Name
    public String getPlayerName()
    {
    return playerName;
    }
    }
    Attached Files Attached Files

  2. #2
    Carl Bochek is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    I have a project due tonight that is almost identical to this, i.e., sort an object of strings and ints both numerically and alphabetically.

    I'll post the e-mail I sent my teacher:


    I can't get my selectSort method to work properly. It seems as though
    there's a problem with how the accessor methods are passing info to the
    variables. FYI, "read the text file into array" and "print as is" are
    working properly and selectSort does something, just not sorting so all
    components are speaking with each other just not in the right way.



    Here is the method:


    public static void selectionSort(Book[] bookData)
    {
    int startScan, index, minIndex, minValue;

    for (startScan = 0; startScan < (bookData.length-1); startScan++)
    {
    minValue = bookData[startScan].getYear();
    minIndex = startScan;
    for(index = startScan + 1; index < bookData.length; index++)
    {
    if (bookData[index].getYear() < minValue)
    {
    minValue = bookData[index].getYear();
    minIndex = index;
    }
    }
    bookData[minIndex] = bookData[startScan];
    bookData[startScan].getYear() = minValue;
    }

    for(int i=0; i<bookData.length; i++)
    System.out.println(bookData[i].getYear() + bookData[i].getTitle());







    This line: bookData[startScan].getYear() = minValue;

    Returns this error:

    Project52.java:121: unexpected type
    required: variable
    found : value
    bookData[startScan].getYear() = minValue;
    ^

    This is obviously the problem. I understand the difference between a
    variable and a value and that you tell a variable to be a value but not
    the other way around. However, I'm not sure how to remedy this.



    For reference here is the class:

    public class Book
    {
    private int year;
    private String title;

    public Book (int y, String t)
    {
    year = y;
    title = t;
    }

    public int getYear()
    {
    return year;
    }

    public String getTitle()
    {
    return title;
    }
    }






    LET'S GET THIS SOLVED!!!!

  3. #3
    Carl Bochek is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    I'm new as well. Hello everyone!!!!

    :)

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Hi Carl,

    You should really start your own thread for your own question. And use code tags: put [CODE] at the start of your code and [/CODE] at the end that way the formatting is preserved.

    Project52.java:121: unexpected type
    required: variable
    found : value
    bookData[startScan].getYear() = minValue;

    Perhaps you mean

    Java Code:
    minValue = bookData[startScan].getYear();
    -------------------------------

    @ddc - sorry about the hijack. Perhaps you would get a better response if you asked a specific question. You say you have having trouble and problems, but you don't give a precise description of what the program does (its output) and what you expected or intended it would do.

  5. #5
    Carl Bochek is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Hi.

    Sorry,

    I posted my question on here as we have identical problems: selection sort of a class that has an int data field and string data field both numerically and alphabetically.

  6. #6
    Carl Bochek is offline Member
    Join Date
    Dec 2010
    Posts
    4
    Rep Power
    0

    Default

    Methods that sort numerically and alphabetically with data strings and ints from an object. I think you'll be able to tell them apart. I hope this helps:

    public static void selectionSort(Book[] bookData)
    {

    int startScan, index, minIndex;
    Book minValue;

    for (startScan = 0; startScan < (bookData.length-1); startScan++)
    {

    minValue = bookData[startScan];
    minIndex = startScan;

    for(index = startScan + 1; index < bookData.length; index++)
    {
    if (bookData[index].getYear() < minValue.getYear())
    {
    minValue = bookData[index];
    minIndex = index;
    }
    }

    bookData[minIndex] = bookData[startScan];
    bookData[startScan] = minValue;
    }

    for(int i = 0; i < bookData.length; i++)
    {
    System.out.println(bookData[i].getYear() + bookData[i].getTitle());
    }

    }






    public static void selectionSort2(Book[] bookData)
    {

    int startScan, index, minIndex;
    Book minValue;

    for (startScan = 0; startScan < (bookData.length-1); startScan++)
    {

    minValue = bookData[startScan];
    minIndex = startScan;

    for(index = startScan + 1; index < bookData.length; index++)
    {

    if (bookData[index].getTitle().compareTo(minValue.getTitle()) <0)
    {
    minValue = bookData[index];
    minIndex = index;
    }
    }

    bookData[minIndex] = bookData[startScan];
    bookData[startScan] = minValue;
    }

    for(int i = 0; i < bookData.length; i++)
    {
    System.out.println(bookData[i].getYear() + bookData[i].getTitle());
    }

    }

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    @OP: Is this a class project working on sort implementations or are you allowed to use Arrays.sort()?

    -Gary-

  8. #8
    gandrew is offline Member
    Join Date
    Nov 2010
    Posts
    5
    Rep Power
    0

    Default

    Dear Sir: You have two problems in your sort section. This looks like a bubble sort. You need to have a temporary holder when you are swapping your array elements. Secondly, you don't need to create a new Player object. You are just re-arranging an existing built array of Player objects. This is why you are losing the Player's associated number. I've added two or three statements and cleared out the comments. It seems to work with this data. Good luck.
    Contents of Team.txt
    -----------------------------
    19
    Jose Bautista
    11
    Alex Gonzalez
    2
    Aaron Hill
    18
    Mike McCoy
    6
    John McDonald
    35
    Lyle Overbay
    --------------
    Sorted data...

    Player Jersey Number
    Aaron Hill 2
    Alex Gonzalez 11
    John McDonald 6
    Jose Bautista 19
    Lyle Overbay 35
    Mike McCoy 18
    --------------------------------------

    System.out.println("Sorted data...");
    Player tempPlayer; // Use this as a temp holder

    for (startScan = 0; startScan < (p.length-1); startScan++)
    {

    minValue = p[startScan].getPlayerName();
    minIndex = startScan;


    for(index = startScan + 1; index < p.length; index++)
    {
    if (p[index].getPlayerName().compareTo(minValue) < 0)
    {
    minValue = p[index].getPlayerName();
    minIndex = index;
    }
    }
    tempPlayer = p[minIndex]; // You need to store this Player

    p[minIndex] = p[startScan];

    p[startScan] = tempPlayer; // Perform this swap
    } // for

    System.out.println();


    System.out.printf("%10s%20s\n", string1, string2);

Similar Threads

  1. sort array >> need help
    By hongi in forum New To Java
    Replies: 4
    Last Post: 04-25-2010, 09:37 PM
  2. Sort String
    By ashin in forum Advanced Java
    Replies: 5
    Last Post: 10-09-2009, 08:08 AM
  3. Need help. Array sort
    By buzz1500 in forum New To Java
    Replies: 3
    Last Post: 11-07-2008, 04:24 AM
  4. Array sort
    By Jeremy720 in forum New To Java
    Replies: 2
    Last Post: 10-07-2008, 11:41 PM
  5. How to sort an array
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-14-2008, 08:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •