-1

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

错误:

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

学生排序.txt

Bren    Ramal
Casi    Ron
Alba    Jordi   

排序数据文件

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); 
    }


    }

学生排序文件

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();
    }
}
4

1 回答 1

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();
    }
}
于 2013-03-04T21:04:40.653 回答