Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8240835
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:37:33+00:00 2026-06-07T20:37:33+00:00

import java.io.BufferedReader; import java.util.Collections; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList;

  • 0
    import java.io.BufferedReader;
    import java.util.Collections;
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.List;
    import com.javaranch.common.TextFileIn;

    public class SortNames 
    {
        public static class CelebrityNamesFile
        {
            public String firstName;
            public String lastName;

            public static class CompareLastName implements Comparator< CelebrityNamesFile >
            {
                @Override
                public int compare( CelebrityNamesFile o1,  CelebrityNamesFile o2 )
                {
                    return o2.lastName.compareTo( o1.lastName );
                }
            }

        public static void main( String[ ] args ) throws IOException
        {

            ArrayList< CelebrityNamesFile > myCelebrityList;
            myCelebrityList = new ArrayList< CelebrityNamesFile >();

            TextFileIn celebrityNamesFile = new TextFileIn( "celebrityNamesFile.txt" );
            boolean doneReadingCelebrityNames = false;
            while ( ! doneReadingCelebrityNames )
            {
                 String oneName = celebrityNamesFile.readLine();
                 if ( oneName == null )
                 {
                     doneReadingCelebrityNames = true;
                 }

$
Eclipse doesn’t like the add statement that follows, to wit: The method add (SortNames.CelebrityNamesFile) in the type ArrayList(SortNames.CelebrityNamesFile)is not applicable for the arguments (String)

                 else
                 {
                     myCelebrityList.add( oneName );
                 }
            }
            celebrityNamesFile.close();

$
And then it doesn’t like my sort statement, to wit:
Bound mismatch: The generic method sort(List T) of type Collections is not applicable for the arguments (ArrayList (SortNames.CelebrityNamesFile)). The inferred type SortNames.CelebrityNamesFile is not a valid substitute for the bounded parameter (T extends Comparable(? super T))

            Collections.sort( myCelebrityList );
            System.out.println( myCelebrityList );

            Collections.sort( myCelebrityList, new CelebrityNamesFile.CompareLastName() );
            System.out.println( myCelebrityList );
}
}

}

What am I doing wrong? I have read many posts here, have read the Java docs regarding comparator, have read Java tutorials regarding comparator. Head First Java, Ivor Horton’s beginning Java 7. Still clueless.

The purpose of the program is to read names from a txt file, add them to an arraylist, print the arraylist in its natural order, sort the arraylist by last name, print the list again.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:37:36+00:00Added an answer on June 7, 2026 at 8:37 pm

    For the first issue, the problem is fairly simple. oneName is a String, and myCelebrityList is a collection of CelebrityNamesFile– which means that you can only add objects to the collection of type CelebrityNamesFile.

    For the second issue, your problem is that the CelebrityNamesFile class does not implement the Comparable interface. Sorting requires that an ordering is implemented for the list element type, e.g. by implementing Comparable for the class or providing a Comparator to the sort.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

package mp1practice; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.logging.Level;
package mp1similar; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer;
package mp1similar; import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.StringTokenizer;
package com.csl.bps.util; import java.awt.Event; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.HashMap;
I have this class: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
I have this code: package com.problemio; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList;
This is my code: import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.util.Scanner; public class
So here is my program, which works ok: import java.io.FileReader; import java.io.BufferedReader; import java.io.IOException;
package src; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Command { public static
import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; public class DateDemo { public static void main(String[]

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.