Results 1 to 6 of 6
Thread: Sorting Problem
- 11-30-2010, 02:25 AM #1
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
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
Show Team Frame ClassJava 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; } }
Team FrameJava Code:import java.io.IOException; class ShowTeamFrame { public static void main(String args[]) throws IOException { new TeamFrame(); } }
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()));"
Fucho.txtJava 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())); } }
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
-
- 11-30-2010, 02:38 AM #3
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
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?
- 11-30-2010, 03:00 AM #4
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
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? } }
- 11-30-2010, 03:01 AM #5
Member
- Join Date
- Jul 2010
- Posts
- 27
- Rep Power
- 0
what is it wrong with teh add(new JLabel(player.getGoals())); statement?
- 11-30-2010, 05:40 AM #6
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Similar Threads
-
sorting problem
By vasug in forum Advanced JavaReplies: 2Last Post: 02-25-2010, 04:55 AM -
Problem: Arrays and Sorting
By Rhez in forum New To JavaReplies: 7Last Post: 02-03-2010, 02:18 PM -
sorting problem...
By mark-mlt in forum New To JavaReplies: 4Last Post: 04-17-2008, 02:15 PM -
sorting problem
By mcal in forum New To JavaReplies: 1Last Post: 02-14-2008, 08:13 AM -
Problem with sorting Table
By sireesha264 in forum Advanced JavaReplies: 0Last Post: 02-08-2008, 02:21 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks