使用冒泡排序按姓氏排序文件?

时间:2013-03-04 20:50:39

标签: java file sorting

在这个程序中,我可以将文件读入数组并拆分数组。然后我有一个数组方法来使用冒号排序按姓氏对文件进行排序。但是,我收到了一个错误。请有人帮助我。

错误:

错误:类StudentSort中的方法BubbleCountry无法应用于给定类型;

StudentSort.txt

Bren    Ramal
Casi    Ron
Alba    Jordi   

SortData文件

import java.io.*;
import java.util.*;


class ShowSort implements Comparable {      
    private String name;
    private String surname;


    public void setName(String name) {
            this.name = name;
    }

    public String getName() {
            return name;
    }

    public void setSurname(String surname) {
            this.surname = surname;
    }

    public String getSurname() {
            return surname;
    }


    public int compareTo(Object Student) throws ClassCastException {
            if (!(Student instanceof ShowSort))
                  throw new ClassCastException("Error");
            String surn = ((ShowSort) Student).getSurname();
            return this.surname.compareTo(surn); 
    }


    }

StudentSort文件

import java.io.*;
import java.util.*;

public class StudentSort {
    StudentSort() {

    int j = 0;
    ShowSort data[] = new ShowSort[3];

    try {
        FileInputStream fstream = new FileInputStream("StudentSort.txt");
        // Use DataInputStream to read binary NOT text.
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String line;
        ArrayList list = new ArrayList();
        while ((line = br.readLine()) != null) {
                list.add(line);
        }
        Iterator itr;
        for (itr = list.iterator(); itr.hasNext();) {
                String str = itr.next().toString();
                String[] splitSt = str.split("\t");
                String name = "", surname = "";
                for (int i = 0; i < splitSt.length; i++) {
                name = splitSt[0];
                surname = splitSt[1];


        }

        BubbleCountry(surname);//This is the issue.

        data[j] = new ShowSort();
        data[j].setName(name);
        data[j].setSurname(surname);


        j++;
        }

        for (int i = 0; i < 3; i++) {
        ShowSort show = data[i];
        String name = show.getName();
        String surname = show.getSurname();

        System.out.println(name + "\t" + surname);

        }



        } catch (Exception e) {
        }

        }           



        private static void BubbleCountry(String[] myarray) {
        String ctry;
        for(int i=0; i<myarray.length; i++) {
            for(int j=0; j<myarray.length-1-i; j++) {
            if(myarray[j].compareTo(myarray[j+1])>0) {
            ctry= myarray[j];
            myarray[j] = myarray[j+1];
            myarray[j+1] = ctry;
                }
            }
        }

        }





    public static void main(String[] args) {
            StudentSort data = new StudentSort();
    }
}

1 个答案:

答案 0 :(得分:1)

您正在呼叫BubbleCountry(surname);但其原型显示

private static void BubbleCountry(String[] myarray)

所以区别就在这里。该函数需要一个字符串数组作为参数,但在调用时您传递的是字符串而不是字符串数组。

import java.io.*;
import java.util.*;

public class StudentSort 
{
    StudentSort() 
    {

        int j = 0;
        ShowSort data[] = new ShowSort[3];

        try 
        {
            FileInputStream fstream = new FileInputStream("StudentSort.txt");
            // Use DataInputStream to read binary NOT text.
            BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

            String line;
            ArrayList<String> list = new ArrayList<String>();
            while ((line = br.readLine()) != null) 
            {
                    list.add(line);
            }   
            Iterator<String> itr = list.iterator();
            int k = 0;
            for (itr = list.iterator(); itr.hasNext();itr.next()) 
            {
                    String str = itr.toString();
                    String[] splitSt = str.split("\t");

                    data[k].setName(splitSt[0]);
                    data[k].setName(splitSt[1]);
                    k++;
            }
            BubbleCountry(data);//This is the issue.


            for (int i = 0; i < 3; i++) 
            {
                ShowSort show = data[i];
                String name1 = show.getName();
                String surname1 = show.getSurname();

                System.out.println(name1 + "\t" + surname1);

            }



        } 
        catch (Exception e) 
        {
        }

    }           



    private static void BubbleCountry(ShowSort[] myarray) 
    {
        ShowSort ctry;
        for(int i=0; i<myarray.length; i++) 
        {
            for(int j=0; j<myarray.length-1-i; j++) 
            {
                if(myarray[j].compareTo(myarray[j+1])>0) 
                {
                    ctry= myarray[j];
                    myarray[j] = myarray[j+1];
                    myarray[j+1] = ctry;
                }
            }
        }

    }





    public static void main(String[] args) 
    {
            StudentSort data = new StudentSort();
    }
}