Results 1 to 6 of 6

Thread: Sorting Problem

  1. #1
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default Sorting Problem

    Hello, im in APCS, and my teacher put us to work on a project with is on Chapter 10 of Java for Dummies. This involves 4 classes and a txt document. The txt document has some statistics about baseball players, and then with help of the 4 classes you show what is on the txt in a window. What my teacher want us to do is to make a txt document in which we put something we like, I choosed the top 10 forwards with the must goals of the Mexican soccer league. First I wrote the name, then the team, and then the amount of goals it scored on the tournament. I wrote the document on ascending order, as my teacher want us to sort it in descending order when it appear on the window. I don't understand what must of the stuff in the program means like JFrame, JLabel, and IOException. I know how to do this, I have done it before with C++ with arrays. But I don't understand how I can do this with arraLists that has objects with different stuff. How can I get to the number of goals from each object of the ArrayList. This is what I have so far, I modified the Player and the Team Frame classes so that they can work on my forwards document:
    Player class
    Java Code:
    import java.text.DecimalFormat;
    
    class Player {   
        private String name;
        private String team;
        private int goals;
      
        public Player(String n, String t, int g) {
            this.name=n;
            this.team=t;
            this.goals=g;
        }
    
        public String getName() {
            return name;
        }
        public String getTeam() {
            return team;
        }
        public int getGoals() {
            return goals;
        }
    }
    Show Team Frame Class
    Java Code:
    import java.io.IOException;
    
    class ShowTeamFrame {
    	
        public static void main(String args[]) throws IOException {
            new TeamFrame();
        }
    }
    Team Frame
    I don't knwo why, but this class has an error, eclipse mark int the addPlayerInfo method, the part that says "add(new JLabel(player.getGoals()));"
    Java Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    
    class TeamFrame extends JFrame {
    
        public TeamFrame() throws IOException {
            Player player;
            Scanner myScanner =new Scanner(new File("Fucho.txt"));
            for (int num = 1; num <= 10; num++) {
                player = new Player(myScanner.nextLine(),myScanner.nextLine(), myScanner.nextInt());
                myScanner.nextLine();
    	    }
    		
            setTitle("Mexico's top 10 forwards");
            setLayout(new GridLayout(6,3));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
         
        void addPlayerInfo(Player player) {
            add(new JLabel(player.getName()));
            add(new JLabel(player.getTeam()));
            add(new JLabel(player.getGoals()));
        }   
    }
    Fucho.txt
    Java Code:
    Carlos Ochoa
    Jaguares
    7
    Sergio Blanco
    Queretaro
    8
    Dario Cvitanich
    Pachuca
    8
    Gabriel Pereyra
    Puebla
    8
    Cristian Gimenez
    Cruz Azul
    9
    Itamar Batista
    Tigres
    9
    Johan Fano
    Atlante
    9
    Vicente Matias Vuoso
    America
    10
    Humberto Suazo
    Monterrey
    12
    Christian Benitez
    Santos
    16

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

  3. #3
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    lol that's an epic fail. There's not one on the code, it uses the scanner class to get the data and then show it on the window. Is it possible to sort it that way? Or how can I add that into an arrayList? Just make one and add the player object?

  4. #4
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    Ok, so think I answered my own question, I make an arrayList and add the player object with all the stuff it has (player name, team, amount of goals). This is why i did(not much):
    Java Code:
    import java.util.Scanner;
    import java.io.File;
    import java.io.IOException;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import java.awt.GridLayout;
    import java.util.ArrayList;
    
    class TeamFrame extends JFrame {
    
        public TeamFrame() throws IOException {
            Player player;
            ArrayList forwards=new ArrayList();
            Scanner myScanner =new Scanner(new File("Fucho.txt"));
            for (int num = 1; num <= 10; num++) {
                player = new Player(myScanner.nextLine(),myScanner.nextLine(), myScanner.nextInt());
                myScanner.nextLine();
                forwards.add(player);
    	    }
    		
            setTitle("Mexico's top 10 forwards");
            setLayout(new GridLayout(6,3));
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pack();
            setVisible(true);
        }
         
        void addPlayerInfo(Player player) {
            add(new JLabel(player.getName()));
            add(new JLabel(player.getTeam()));
            add(new JLabel(player.getGoals()));//I keep having an error in here,  //what is it wrong?
        }   
    }

  5. #5
    Freakzoyd is offline Member
    Join Date
    Jul 2010
    Posts
    27
    Rep Power
    0

    Default

    what is it wrong with teh add(new JLabel(player.getGoals())); statement?

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

    Default

    The JLabel constructor wants a String argument, but you are supplying an int (which is what getGoals() is returning).

    In general a "cannot find symbol" message means "check your spelling for typos, check the scope, check the argument types".

Similar Threads

  1. sorting problem
    By vasug in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2010, 04:55 AM
  2. Problem: Arrays and Sorting
    By Rhez in forum New To Java
    Replies: 7
    Last Post: 02-03-2010, 02:18 PM
  3. sorting problem...
    By mark-mlt in forum New To Java
    Replies: 4
    Last Post: 04-17-2008, 02:15 PM
  4. sorting problem
    By mcal in forum New To Java
    Replies: 1
    Last Post: 02-14-2008, 08:13 AM
  5. Problem with sorting Table
    By sireesha264 in forum Advanced Java
    Replies: 0
    Last Post: 02-08-2008, 02:21 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
  •