Axwell Smith
Axwell Smith

Reputation: 57

Not using the Comparable interface in the right way

Been looking for an answer to this but it seems like my problem is more specific than others.

So i have two classes and one interface. The interface is called "Comparable" and i know that the interface does have its own method, but we will get to that in a sec.

What i have is the classes called "cityMain", "City2" and the interface "Comparable"

What my program is doing right now is that it reads from a txt file with something like:

75242;Uppsala 90325;Umeå 96133;Boden 23642;Höllviken 35243;Växjö 51000;Jönköping 72211;Västerås etc

The semicolon gets away and the integers and strings split into two. The numbers are zip codes and well the thing next to it is just a name of some states in sweden. After reading it from the "mainCity" i am actually sorting it so that the top zip with it's name gets to be at the top, so from the least number to the largest.

Then when it gets to read it, it goes to the "City2" class and from there it just goes by every method, one zip and one state at the time.

HOWEVER: Atm i am actually calling everything from my main and NOT from my interface.

It should go like this "mainCity" --> "Comparable" --> "City2". The program works as it is but i want it to be correct!

I have tried to go from the "Comparable" and then call it like that but it didn't work nor did it give me an error. Note, again: That Comparable does has its own method but i have not used it due to the fact that i wouldn't know of how to apply it as it looks right now.

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


public class cityMain {

private static BufferedReader r;

public static void main(String[] args) {
    int Z = 0;
    String C = null;
    try {
        ArrayList<City2> City = new ArrayList<City2>();

        FileReader file = new FileReader("C:\\Users\\me\\Desktop\\BB.dat");
        r = new BufferedReader(file);
        String currLine;
        while ((currLine = r.readLine()) != null) {

            if (currLine.trim().length() > 0) {
                String[] split = currLine.split(";");
                Z = (Integer.parseInt(split[0]));
                C = (split[1]);
                City.add(new City2(Z, C));
            }
        }
        Collections.sort(City, (c1, c2) -> c1.getZipCode() - c2.getZipCode());

        for (int i = 0; i < City.size(); i++) {
            System.out.println(City.get(i).getZipCode() + " " + City.get(i).getCityName());

        }

    } catch (IOException e) {
        System.out.println("Erorr : " + e);
    }

}

}


HERE IS A DIFFERENT CLASS:

public class City2 implements Comparable {

private int zipCode;
private String cityName;

public City2(int zipCode, String cityName) {
    this.zipCode = zipCode;
    this.cityName = cityName;
}

public int getZipCode() {
    return zipCode;
}

public String getCityName() {
    return cityName;
}

public void addAndPrint() {

}

}

HERE IS THE INTERFACE:

public interface Comparable {

public int getZipCode();

public String getCityName();

public void addAndPrint(); 

}

What i should get, which i already do get but not in the way that i am supposed to do it!

23642 Höllviken 35243 Växjö 51000 Jönköping 72211 Västerås 75242 Uppsala 90325 Umeå 96133 Boden

Anything would be greatly appreciated right now!

Upvotes: 1

Views: 154

Answers (3)

Axwell Smith
Axwell Smith

Reputation: 57

Okay so...

Now it is almost 100% Complete!

But i am getting the same answer over and over again...

Ideas? It is like the prints repeat but the last lines are actually correct....

Main class: public class cityMain

private static BufferedReader r;
public static ArrayList<City2> City = new ArrayList<City2>();
public static void main(String[] args) {
    int Z = 0;
    String C = null;
    try {
        FileReader file = new FileReader("C:\\Users\\karwa\\Desktop\\BB.dat");
        r = new BufferedReader(file);
        String currLine;
        while ((currLine = r.readLine()) != null) {
            City.sort(Comparator.comparing(City2::getZipCode));
            if (currLine.trim().length() > 0) {
                String[] split = currLine.split(";");
                Z = (Integer.parseInt(split[0]));
                C = (split[1]);
                City2 d = new City2(Z, C);
                City.add(d);
                d.print();
            }
        }

    } catch (IOException e) {
        System.out.println("Erorr : " + e);
    }

}

Second class:

class City2 implements Comparable<City2> {

private int zipCode;
private String cityName;

public City2(int zipCode, String cityName) {
    this.zipCode = zipCode;
    this.cityName = cityName;
}

public int getZipCode() {
    return zipCode;
}

public String getCityName() {
    return cityName;
}

@Override
public int compareTo(City2 city) {
    return this.getZipCode() - city.getZipCode();
}

public void print() {

    for (int i = 0; i < cityMain.City.size(); i++) {
        System.out.println(cityMain.City.get(i).getZipCode() + " " + cityMain.City.get(i).getCityName());
    }

}
}

Upvotes: 0

Nicholas K
Nicholas K

Reputation: 15443

If you explicitly require to use the Comparable interface then change your City2 class to :

public class City2 implements Comparable<City2> {

    private int zipCode;
    private String cityName;

    public City2(int zipCode, String cityName) {
        this.zipCode = zipCode;
        this.cityName = cityName;
    }

    public int getZipCode() {
        return zipCode;
    }

    public String getCityName() {
        return cityName;
    }

    public void addAndPrint() {

    }

    @Override
    public int compareTo(City2 city) {
        return this.getZipCode() - city.getZipCode();
    }

}

Observe that :

  1. Here we are implementing the Comparable<City2> rather than the raw type Comparable
  2. Overriding the compareTo() to compare zip-codes as required.
  3. There is no need for you to explicitly create an interface Comparable, but instead just implement the one java provides.

Upvotes: 1

Karol Dowbecki
Karol Dowbecki

Reputation: 44980

If you plan to use Java standard library sorting you need to use standard interfaces. You need either collection elements to implement java.lang.Comparable or a separate java.lang.Comparator object.

It would be best to delete or rename your own version of Comparator, it makes unclear which one is actually used when looking at your code because java.lang classes are imported automatically.

You have already created a java.lang.Comparator with below (c1, c2) lambda:

Collections.sort(City, (c1, c2) -> c1.getZipCode() - c2.getZipCode());

although you could have written it also as:

Collections.sort(City, Comparator.comparingInt(City2::getZipCode));

Or using List.sort:

City.sort(Comparator.comparingInt(City2::getZipCode));

Upvotes: 1

Related Questions