小弟是新手,不太清楚哪里错了,请各位指教
代码如下
import java.util.Scanner;
import java.io.*;class MergeSortAlgorithms{
final static String MAXVALUE = "zzzzzzzzzzzzz";
static People[] L;
static People[] R;
public static void Merge(People[] A,int p,int q,int r){
int n1=q-p;
int n2=r-q+1;
L = new People[n1+1];
R = new People[n2+1];
for(int i = 0;i < n1;i++)
L[i] = A[p+i];
for(int j = 0;j < n2;j++)
R[j] = A[q+j];
L[n1].setName(MAXVALUE);
R[n2].setName(MAXVALUE);
int i = 0,j = 0;
for(int k = p; k <= r ;k++){
String temp = L[i].getName();
if(L[i].CompareTo(R[j])<1){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
public static void MergeSort(People[] A,int p,int r){
int q;
if(p<r){
q = (p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q+1,r);
}
}
}class AddressBook{
private People[] entry;
private static int count;
public AddressBook(){
this(25);
}
public AddressBook(int size){
entry = new People[size];
}
public void add(People newPeople){
if(count == entry.length)
enlarge();
entry[count] = newPeople;
count++;
}
public boolean delete(String searchName){
int i;
if (findIndex(searchName) == -1)
return false;
else{
while(findIndex(searchName) != -1){
for(i = findIndex(searchName);i < (count-1);i++)
entry[i] = entry[i+1];
count--;
}
return true;
}
}
public People search(String searchName){
if (findIndex(searchName) == -1)
return null;
else
return entry[findIndex(searchName)];
}
public void sort(){
MergeSortAlgorithms.MergeSort(entry,0,entry.length-1);
}
public void enlarge(){
int i;
People newentry[] = new People [(int)(entry.length * 1.5)];
for(i=0;i<entry.length;i++)
newentry[i] = entry[i];
entry = newentry;
}
public int findIndex(String searchName){
int i;
for (i=0;i<count;i++)
if(entry[i].getName().compareTo(searchName) == 0)
return i;
return -1;
}
public void saveRecord()throws IOException{
int i;
File file = new File("record.txt");
PrintWriter output = new PrintWriter(file);
for(i=0;i<count;i++)
output.print(entry[i].toString());
output.close();
}
public void clearRecord()throws IOException{
int i;
File file = new File("record.txt");
PrintWriter output = new PrintWriter(file);
output.close();
}
public void restoreRecord(){
String temp;
People newPeople = new People();
Scanner input = new Scanner("record.txt");
while (input.hasNext()){
temp = input.next();
newPeople.setName(input.next());
temp = input.next();
newPeople.setSex(input.next().charAt(0));
temp = input.next();
newPeople.setAge(input.nextInt());
this.add(newPeople);
}
}
}class People{
private String name;
private int age;
private char sex;
private static int count;
public People(){
this("",'M',0);
}
public People(String name, char sex, int age){
setName(name);
setSex(sex);
setAge(age);
count++;
}
public void setName(String name){
this.name = name;
}
public void setSex(char sex){
this.sex = sex;
}
public void setAge(int age){
this.age =age;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
public int getAge(){
return age;
}
public int getCount(){
return count;
}
public String toString(){
return "\nName: "+name+"\nSex: "+sex+"\nAge: "+age;
}
public int CompareTo(People People){
if (this.name.compareTo(People.name) > 0)
return -1;
else if (this.name.compareTo(People.name) == 0)
return 0;
else return 1;
}
}public class Test{
public static void main(String ag[])throws IOException{
AddressBook a1 = new AddressBook();
a1.restoreRecord();
a1.sort();
a1.saveRecord();
}
}
另外附上测试数据(record.txt里的数据):
Name: Roy
Sex: M
Age: 19
Name: Mary
Sex: M
Age: 19
Name: Gary
Sex: M
Age: 19
Name: Wu
Sex: M
Age: 19
Name: Chary
Sex: M
Age: 19
Name: Ada
Sex: W
Age: 20
Name: Jie
Sex: M
Age: 19
Name: Wei
Sex: M
Age: 19
Name: Nick
Sex: M
Age: 19

解决方案 »

  1.   

    你所说错误是这样改:
                Scanner input = null;
    try {
    input = new Scanner(new File("record.txt"));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    }但我改后,还有一些小错误,lz自己调了,呵呵
      

  2.   

    没有语法错误,是运行错误
    Exception in thread "main" java.lang.NullPointerException
            at MergeSortAlgorithms.Merge(Test.java:17)
            at MergeSortAlgorithms.MergeSort(Test.java:37)
            at MergeSortAlgorithms.MergeSort(Test.java:35)
            at MergeSortAlgorithms.MergeSort(Test.java:35)
            at MergeSortAlgorithms.MergeSort(Test.java:35)
            at MergeSortAlgorithms.MergeSort(Test.java:35)
            at AddressBook.sort(Test.java:77)
            at Test.main(Test.java:180)
    看起来是MergeSort的问题,可是我觉得没有错啊。
      

  3.   

    L[n1].setName(MAXVALUE);
    R[n2].setName(MAXVALUE);
    L[n1],R[n2]有可能是null,直接调用出现空指针异常if(L[n1] != null)//
            L[n1].setName(MAXVALUE);
            if(R[n2] != null)//
            R[n2].setName(MAXVALUE);
            int i = 0,j = 0;
            for(int k = p; k <= r ;k++){
             if(L[i] == null)//
             continue;//
                String temp = L[i].getName();
                if(L[i].CompareTo(R[j])<1){
                    A[k] = L[i];
                    i++;
                }
                else{
                    A[k] = R[j];
                    j++;
                }
            }
      

  4.   

    用debug调试 跟踪一下 很快就能找到问题的!
      

  5.   

    空指针异常
    检查下是不是L[n1],R[n2]里面没有东西
      

  6.   

    Exception in thread "main" java.lang.NullPointerException
    at _10_15.MergeSortAlgorithms.Merge(Test.java:19)
    at _10_15.MergeSortAlgorithms.MergeSort(Test.java:40)
    at _10_15.MergeSortAlgorithms.MergeSort(Test.java:38)
    at _10_15.MergeSortAlgorithms.MergeSort(Test.java:38)
    at _10_15.MergeSortAlgorithms.MergeSort(Test.java:38)
    at _10_15.MergeSortAlgorithms.MergeSort(Test.java:38)
    at _10_15.AddressBook.sort(Test.java:80)
    at _10_15.Test.main(Test.java:183)
    Scanner input = new Scanner("record.txt");
    这样用Scanner的话,后面只会读取字符串"record.txt"这几个单词的内容,而不是外部文件的内容,改后程序就没有那个错误了,但是会出现空指针异常,应该是逻辑上有点错误的。
      

  7.   

    Exception in thread "main" java.lang.NullPointerException
            at People.CompareTo(Test.java:172)
            at MergeSortAlgorithms.Merge(Test.java:25)
            at MergeSortAlgorithms.MergeSort(Test.java:41)
            at MergeSortAlgorithms.MergeSort(Test.java:39)
            at MergeSortAlgorithms.MergeSort(Test.java:40)
            at MergeSortAlgorithms.MergeSort(Test.java:39)
            at AddressBook.sort(Test.java:81)
            at Test.main(Test.java:184)
      

  8.   


    // 执行你的程序,到这步可以看到 p=0 ,q=1,r=1
            int n1 = q - p;// 执行后 n1=1
            int n2 = r - q + 1;// 执行后 n2=1
            L = new People[n1 + 1];// new People[2]
            R = new People[n2 + 1];// new People[2]
            for (int i = 0; i < n1; i++)
                // 就是此处出的问题,此时你n1还是为1,而你的循环条件是i = 0; i < n1,那么接下来,只能L[0]被初始化
                L[i] = A[p + i];
            for (int j = 0; j < n2; j++)
                // 此处也会有同样的问题
                R[j] = A[q + j];        L[n1].setName(MAXVALUE);// 此处L[n1]就是L[1],你还没有初始化就用了,所以就空指针了
            R[n2].setName(MAXVALUE);// 同理这个地方也会有问题