13 Replies - 289 Views - Last Post: 06 October 2013 - 09:25 PM Rate Topic: -----

#1 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Question about printing certain items from array list

Posted 05 October 2013 - 07:00 AM

I have an array length of 100, and the content are like below, with name, wins, losses, etc...

Texas 867 334 33 0.71596 120 1234 Big 12

And I want to know how can I print out 15 teams(like above) with the most wins. The only thing I hard-coded is the array of 100, all the content(name, wins, losses...) are in excel.

I also wrote a class(just a typical class), a searching and sorting class. But if you need to see any of my code do tell.

Is This A Good Question/Topic? 0
  • +

Replies To: Question about printing certain items from array list

#2 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Question about printing certain items from array list

Posted 05 October 2013 - 07:16 AM

Quote

all the content(name, wins, losses...) are in excel.
Save it as csv and process that in your Java app, which can then process directly from file

This post has been edited by g00se: 05 October 2013 - 07:17 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#3 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Question about printing certain items from array list

Posted 05 October 2013 - 07:18 AM

Have you made a Team class? Start by implementing one if you haven't already
Sorting is easily done afterwards
The Arrays class has several sort methods that you can use
Implement a Comparator that will tell the sort algorithm how to sort the teams
Team[] teams = ...;
Comparator<Team> winsComparator = new WinComparator();
Arrays.sort(teams, winsComparator);


All of your logic will need to be in the Comparator
Was This Post Helpful? 1
  • +
  • -

#4 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 05 October 2013 - 07:34 AM

View Postg00se, on 05 October 2013 - 07:16 AM, said:

Quote

all the content(name, wins, losses...) are in excel.
Save it as csv and process that in your Java app, which can then process directly from file

Oh yeah, I already did all that. Sorry for the confusion.
Was This Post Helpful? 0
  • +
  • -

#5 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 05 October 2013 - 07:45 AM

View PostCasiOo, on 05 October 2013 - 07:18 AM, said:

Have you made a Team class? Start by implementing one if you haven't already
Sorting is easily done afterwards
The Arrays class has several sort methods that you can use
Implement a Comparator that will tell the sort algorithm how to sort the teams
Team[] teams = ...;
Comparator<Team> winsComparator = new WinComparator();
Arrays.sort(teams, winsComparator);


All of your logic will need to be in the Comparator

Yup, already got Team class. In fact, I have something similar to yours:
public static void main(String[] args) {
		Scanner inputFile;
		String inputLine;
		Scanner in = new Scanner(System.in);
		CollegeFootballTeam[] teams = new CollegeFootballTeam[126];

		int ii = 0;
		
		try {
			inputFile = new Scanner(new File("src/Project5/cfb_list.csv"));
			while (inputFile.hasNext()) {
				inputLine = inputFile.nextLine();
				String[] splitLine = inputLine.split(",");

				teams[ii] = new CollegeFootballTeam(
						splitLine[0],
						Integer.parseInt(splitLine[1]),
						Integer.parseInt(splitLine[2]),
						Integer.parseInt(splitLine[3]),
						Float.parseFloat(splitLine[4]),
						Integer.parseInt(splitLine[5]),
						Integer.parseInt(splitLine[6]),
						splitLine[7]);
				ii ++;
			}
		} catch (FileNotFoundException e) {
			System.out.println("Error Reading file");
		}

		System.out.print("Enter a school: ");
		String SearchString = in.nextLine();
		
		Sorting.insertionSort(teams);
						
        CollegeFootballTeam searchTeam = new CollegeFootballTeam(SearchString, 0, 0, 0, 0.00, 0, 0, "");
		        
        CollegeFootballTeam foundTeam = (CollegeFootballTeam) Searching.binarySearch(teams, searchTeam); 
        
        if (foundTeam != null)
        	System.out.println(foundTeam);
        else
        	System.out.println("School not found.");
        
  
        
     
	}

But it's used to do a different job. I just don't know how I should print out 15 teams with the most wins? And yeah, I know I should use generic up there, but I didn't learn much about it yet so will leave it there until I know how use generic.
Was This Post Helpful? 0
  • +
  • -

#6 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Question about printing certain items from array list

Posted 05 October 2013 - 09:57 AM

Quote

I just don't know how I should print out 15 teams with the most wins?

CasiOo has already answered that. Check out API docs for java.util.Comparator

This post has been edited by g00se: 05 October 2013 - 09:58 AM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#7 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 06 October 2013 - 12:04 AM

I just added this to my CFT class:
public static Comparator<CollegeFootballTeam1> winsComparator = new Comparator<CollegeFootballTeam1>() {

		public int compare(CollegeFootballTeam1 win1, CollegeFootballTeam1 win2) {

			Integer teamWin1 = win1.getWins();
			Integer teamWin2 = win2.getWins();

			return teamWin1.compareTo(teamWin2);

			
		}

	};

And put this in my driver:
Arrays.sort(teams, CollegeFootballTeam1.winsComparator);

but it highlighted the sort and give me an error saying that: The method sort(T[], Comparator<? super T>) in the type Arrays is not applicable for the arguments (CollegeFootballTeam[], Comparator<CollegeFootballTeam1>)
Can someone help me out a little here, I'm kinda at lost.
Was This Post Helpful? 0
  • +
  • -

#8 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Question about printing certain items from array list

Posted 06 October 2013 - 01:44 AM

Quote

CollegeFootballTeam1
What is that? It should be CollegeFootballTeam
Was This Post Helpful? 0
  • +
  • -

#9 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 06 October 2013 - 04:19 AM

No, it really is CollegeFootballTeam1. I usually create multiple classes to test and try out different things on them, don't wanna mess with the one that already worked. If there's nothing else, then something probably wrong with my eclipse again.
Was This Post Helpful? 0
  • +
  • -

#10 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Question about printing certain items from array list

Posted 06 October 2013 - 04:24 AM

Quote

Arrays.sort(teams, CollegeFootballTeam1.winsComparator);
What type is 'teams'? It would seem that it's CollegeFootballTeam, in which case you've used the wrong type. Both argument types must be the same contained class

This post has been edited by g00se: 06 October 2013 - 04:27 AM

Was This Post Helpful? 1
  • +
  • -

#11 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 06 October 2013 - 08:14 AM

Again, sorry about my carelessness. Anyways, I decided to print out all 126 items and it able to sort all the teams' wins. So another question is how do I limit to just 15 highest wins?
Was This Post Helpful? 0
  • +
  • -

#12 g00se  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 2735
  • View blog
  • Posts: 11,522
  • Joined: 20-September 08

Re: Question about printing certain items from array list

Posted 06 October 2013 - 08:16 AM

Sort the array in reverse and take the first 15 elements
Was This Post Helpful? 0
  • +
  • -

#13 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1401
  • View blog
  • Posts: 3,109
  • Joined: 05-April 11

Re: Question about printing certain items from array list

Posted 06 October 2013 - 08:18 AM

So you now have the array sorted
The 15 first items in the array (Or last depending how you sorted) will be the teams with the most wins
Index 0 - 14 in the array will be the teams you need to print :) Use a loop
Was This Post Helpful? 1
  • +
  • -

#14 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about printing certain items from array list

Posted 06 October 2013 - 09:25 PM

Alright, thank you so much guys. Everything is good to go.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1