compareTo()不按我想要的方式工作

时间:2014-02-18 03:58:38

标签: java arrays string compareto

我正在写一个程序而且我被困在一个部分,我必须用我的“flight900”文件更改我的第一个数组中某些字符串的结束数。第一个阵列已经按航班号排序,每个人都有-1个行李。我正在使用二进制搜索算法在我的数组中搜索flight900名称,以使它们与字符串值匹配。 我相信我的if-else连锁店出了点问题。永远不满足if条件。我找不到解决方法。这是我的二分搜索算法。

public class binarySort 
{
    public static void binary(String[] array, String value, String array2[])
    {

        //Initializing
        int middle, first=0, last=67, position=-1, counter=-1;
        int result=0,a=0,b=0,c=0,d=0;
        boolean found = false;

        //binary search
        while((!found) && (first<=last))
        {
            middle = ((first + last)/2);

            //These are STRINGS to get the first name from the flight900 list
            String str0 = array[middle].substring(6,9);
            String str1 = array[middle].substring(6,10);
            String str2 = array[middle].substring(6,11);
            String str3 = array[middle].substring(6,12);
            String str4 = array[middle].substring(6,13);

            int answer0=0, answer1=0, answer2=0, answer3=0, answer4=0;

            //THESE ARE INTEGERS
            //compareTo(); returns 0 if both values are equal, a positive number of the first is bigger,
            //and a negative number of the first is smaller
            answer0 = str0.compareTo(value);
            answer1 = str1.compareTo(value);
            answer2 = str2.compareTo(value);
            answer3 = str3.compareTo(value);
            answer4 = str4.compareTo(value);
            counter++;

            /*I believe something is wrong in this if-else chain.*/
            if((answer0==0)||(answer1==0)||(answer2==0)||(answer3==0)||(answer4==0))
            {
                found = true;
                position = middle;
                array[position] = array2[counter];

                result = str0.compareTo(value);
            }
            else if((result >= 0)||(a >= 0)||(b >= 0)||(c >= 0)||(d >= 0))
            {
                last = (middle -1);
            }
            else
            {
                first = (middle + 1);
            }   
        }
    }
}

这是我的测试课。一切似乎都在测试器类中工作,它只是二进制部分不起作用。我决定在另一个类中进行二进制搜索,因为它开始变得无组织。

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

public class airplane
{
    public static void main(String[] args)
    {

        String FileName = "Reservationrecords.txt";//FileName is the text file name
        String line;                               //line is the line I read
        String[] allPassengers = new String[67];   //Store "line" in this array to make the list of passengers
        String noOfCheckInBaggage= "-1";           //Everyone starts at -1 bags

        //Try to read the file and put it into "allPassengers" array
        //If can't read file, prints out error
        try 
        {
            BufferedReader input = new BufferedReader(new FileReader(FileName)); //Reads files name
            for(int i=0; i<67; i++)
            {
                    allPassengers[i] = input.readLine();                         //Populates the array
                    allPassengers[i] += noOfCheckInBaggage;                      //Adds "-1" at the end of the element
            }
            input.close();                                                       //You open a file, read a file, then you must close the file
        } 
        catch (Exception e) 
        {
                System.out.println("Error while reading the file: " + e.getMessage()); //Prints out the error message
        }


        //This section sorts the "allPassengers" array
        int result;                 //Holds -1 or 0
        int minIndex;               //Minimum low value
        String temp;                //Holds a temporary String which is used for swapping
        String minValue;            //Holds the smallest String

        //Insertion sort algorithm for sorting Strings.
        for(int startScan = 0; startScan < allPassengers.length-1; startScan++)
        {
            minIndex = startScan;
            minValue = allPassengers[minIndex];

            //For-loop to sort the array
            for(int index = startScan + 1; index < allPassengers.length; index++)
            {
                result = allPassengers[index].compareTo(minValue);
                if(result < 0)
                {

                    minValue = allPassengers[index];
                    minIndex = index;
                }
            }

            //Swap function
            {
                temp = allPassengers[minIndex];
                allPassengers[minIndex] = allPassengers[startScan];
                allPassengers[startScan] = temp;
            }
        }

        String[] flight900 = new String[14];

        try 
        {
            BufferedReader input = new BufferedReader(new FileReader("flight900.txt")); //Reads files name
            for(int i=0; i<14; i++)
            {
                flight900[i] = input.readLine();                     //Populates the array
            }
            input.close();                                           //You open a file, read a file, then you must close the file
        } 
        catch (Exception e) 
        {
                System.out.println("Error while reading the file: " + e.getMessage()); //Prints out the error message
        }

        binarySort.binary(allPassengers, "Barnett", flight900);
        binarySort.binary(allPassengers, "Berndt", flight900);
        binarySort.binary(allPassengers, "Carroll", flight900);
        binarySort.binary(allPassengers, "Chen", flight900);
        binarySort.binary(allPassengers, "Vijay", flight900);
        binarySort.binary(allPassengers, "Chu", flight900);
        binarySort.binary(allPassengers, "Zheng", flight900);
        binarySort.binary(allPassengers, "Costello", flight900);
        binarySort.binary(allPassengers, "Cusumano", flight900);
        binarySort.binary(allPassengers, "Dougherty", flight900);
        binarySort.binary(allPassengers, "Dyer", flight900);
        binarySort.binary(allPassengers, "Eppinger", flight900);
        binarySort.binary(allPassengers, "Fernandez", flight900);
        binarySort.binary(allPassengers, "Frank", flight900);


        for(int index=0; index<67; index++)
        {
            System.out.println(allPassengers[index]);
        }


    }
}

这是我在第一个数组中读到的原始文件。

    AZ495   Malone, Thomas  A1213878    Scorpius
    AZ386   Granja, Joao    A4567897    Canis   
    AZ531   EL Miligi, Haytham  C3947390    Canis   Green peace activist
    AZ900   Franck, Travis  C6755437    Canis   IBN chief researcher
    AZ531   Dhanjal, Surinder   C9219437    Canis   
    AZ495   Mende, Paul D1219097    Cancer  
    AZ386   Hadzima, Joseph D3333123    Virgo   
    AZ874   Francesco, Monticone    D6544678    Eridanus    
    AZ900   Cusumano, Michael   E4543409    Eridanus    
    AZ874   Zhuoran, Zhao   E6577779    Crux    
    AZ531   Barwreck, Obama E6666666 EridanusHead of international spy ring
    AZ531   Misseille, Obama    E6666667 Eridanus Advisor of Barwreck         Obama
    AZ874   Akinc, Bridget  F5456776    Virgo   
    AZ386   Jay, Jason  F5459908    Draco   
    AZ874   Asquith, Paul   F5676655    Aquarius    
    AZ495   Nachtrieb, Robert   F7878333    Leo 
    AZ900   Berndt, Ernst   G5643211    Taurus  
    AZ900   Carroll, John   G5656789    Cepheus 
    AZ531   Cummer, Rick    H3947399    Leo 
    AZ874   Sarfraz, Khurshid   I8767890    Leo 
    AZ386   Bill, Gate  I8978999    Taurus  
    AZ874   Zheng, Wang     J8767765    Eridanus    
    AZ874   Vijay, Garg     K7686546    Capricornus 
    AZ495   Bent, Bernanke  K8888909    Taurus  Inventor of Hot Money Theory
    AZ900   Fernandez, Roberto  L7077765    Leo 
    AZ531   O'Neil, Kevin   L8495849    Virgo   
    AZ531   Alexis, Kwasinski   L9875676    Corona  CEO of Koyota Motor
    AZ531   Babinchuk, Wayne    N9847293    Cancer  
    AZ531   Alan, Bovik O5646324    Draco   
    AZ386   Isaacs, William O8796234    Cepheus 
    AZ900   Costello, Anna  P7878665    Eridanus    
    AZ495   Gregory, Nathaniel  P8796543    Eridanus    
    AZ495   Kritzman, Mark  P9090888    Eridanus    
    AZ874   Ackerman, Joshua    P9809777    Virgo   
    AZ874   Andreas, Gerstlauer P9868568    Cepheus 
    AZ386   Kellogg, Suzuki Q1113133    Corona  Founder of Sonny
    AZ874   Jeff, Andrews   Q2123443    Cancer  United Nation negotiator
    AZ495   Klein, Janice   Q3773331    Cepheus 
    AZ495   Kogan, Leonid   Q7777077    Capricornus 
    AZ531   Lee, Mahnhoon   Q9487400    Taurus  
    AZ900   Chu, Zen    R2000098    Cepheus 
    AZ900   Vijay, Garg R6567123    Corona  
    AZ531   Park, Andrew    S4957459    Aquarius    
    AZ874   Sarfraz, Khurshid   S5654678    Canis   
    AZ531   Sharma, Mridula S7654566    Taurus  Queen of Ingland
    AZ874   Deji, Akinwande S7678769    Canis   
    AZ495   Little, John    S7878653    Crux    
    AZ531   Rahman, Musfiq  S8765567    Corvus  
    AZ386   Hynes, Tod  T2323111    Taurus  
    AZ900   Eppinger, Steven    T2343111    Cancer  
    AZ386   Jackie, Stewart T7773123    Corvus  
    AZ874   Andrea, Alu T7777656    Taurus  
    AZ900   Dyer, Barbara   T8888765    Crux    
    AZ900   Zheng, Wang U1321254    Capricornus 
    AZ531   Kwiatkowska, Mila   U3845738    Virgo   
    AZ386   Michael, Schumacher U5454231    Aquarius    
    AZ900   Barnett, Arnold U7678765    Corvus  
    AZ386   Homer, Jack U8897529    Virgo   
    AZ874   Bei, Yu U8978999    Scorpius    
    AZ900   Dougherty, Jim  W1232121    Scorpius    
    AZ531   Mikhail, Belkin     W9876661    Cepheus 
    AZ495   Roberts, Edward X3434222    Virgo   
    AZ495   Reagans, Ray    X6321235    Canis   
    AZ531   Ahmed, Faheem   X6836493    Crux    
    AZ531   A, Mohd X9878038    Scorpius    
    AZ495   O'Shea, Rory    Y1212332    Canis   
    AZ900   Chen, Elaine    Y7876780    Draco   Nobel prize (Beauty)

只是来了,这是我想要更改结束号码的flight900文本文件。

    Barnett, Arnold U7678765    0
    Berndt, Ernst   G5643211    1
    Carroll, John   G5656789    1
    Chen, Elaine    Y7876780    1
    Vijay, Garg R6567123    -1
    Chu, Zen    R2000098    2
    Zheng, Wang U1321254    0
    Costello, Anna  P7878665    1
    Cusumano, Michael   E4543409    1
    Dougherty, Jim  W1232121    -1
    Dyer, Barbara   T8888765    2
    Eppinger, Steven    T2343111    -1
    Fernandez, Roberto  L7077765    0
    Franck, Travis  C6755437    2

运行之后,我得到了排序列表的输出,没有任何数字被更改。

AZ386   Bill, Gate  I8978999    Taurus  -1
AZ386   Granja, Joao    A4567897    Canis   -1
AZ386   Hadzima, Joseph D3333123    Virgo   -1
AZ386   Homer, Jack U8897529    Virgo   -1
AZ386   Hynes, Tod  T2323111    Taurus  -1
AZ386   Isaacs, William O8796234    Cepheus -1
AZ386   Jackie, Stewart T7773123    Corvus  -1
AZ386   Jay, Jason  F5459908    Draco   -1
AZ386   Kellogg, Suzuki Q1113133    Corona  Founder of Sonny-1
AZ386   Michael, Schumacher U5454231    Aquarius    -1
AZ495   Bent, Bernanke  K8888909    Taurus  Inventor of Hot Money Theory-1
AZ495   Gregory, Nathaniel  P8796543    Eridanus    -1
AZ495   Klein, Janice   Q3773331    Cepheus -1
AZ495   Kogan, Leonid   Q7777077    Capricornus -1
AZ495   Kritzman, Mark  P9090888    Eridanus    -1
AZ495   Little, John    S7878653    Crux    -1
AZ495   Malone, Thomas  A1213878    Scorpius-1
AZ495   Mende, Paul D1219097    Cancer  -1
AZ495   Nachtrieb, Robert   F7878333    Leo -1
AZ495   O'Shea, Rory    Y1212332    Canis   -1
AZ495   Reagans, Ray    X6321235    Canis   -1
AZ495   Roberts, Edward X3434222    Virgo   -1
AZ531   A, Mohd X9878038    Scorpius    -1
AZ531   Ahmed, Faheem   X6836493    Crux    -1
AZ531   Alan, Bovik O5646324    Draco   -1
AZ531   Alexis, Kwasinski   L9875676    Corona  CEO of Koyota Motor-1
AZ531   Babinchuk, Wayne    N9847293    Cancer  -1
AZ531   Barwreck, Obama E6666666    Eridanus    Head of international spy ring-1
AZ531   Cummer, Rick    H3947399    Leo -1
AZ531   Dhanjal, Surinder   C9219437    Canis   -1
AZ531   EL Miligi, Haytham  C3947390    Canis   Green peace activist-1
AZ531   Kwiatkowska, Mila   U3845738    Virgo   -1
AZ531   Lee, Mahnhoon   Q9487400    Taurus  -1
AZ531   Mikhail, Belkin     W9876661    Cepheus -1
AZ531   Misseille, Obama    E6666667    Eridanus    Advisor of Barwreck Obama-1
AZ531   O'Neil, Kevin   L8495849    Virgo   -1
AZ531   Park, Andrew    S4957459    Aquarius    -1
AZ531   Rahman, Musfiq  S8765567    Corvus  -1
AZ531   Sharma, Mridula S7654566    Taurus  Queen of Ingland-1
AZ874   Ackerman, Joshua    P9809777    Virgo   -1
AZ874   Akinc, Bridget  F5456776    Virgo   -1
AZ874   Andrea, Alu T7777656    Taurus  -1
AZ874   Andreas, Gerstlauer P9868568    Cepheus -1
AZ874   Asquith, Paul   F5676655    Aquarius    -1
AZ874   Bei, Yu U8978999    Scorpius    -1
AZ874   Deji, Akinwande S7678769    Canis   -1
AZ874   Francesco, Monticone    D6544678    Eridanus    -1
AZ874   Jeff, Andrews   Q2123443    Cancer  United Nation negotiator-1
AZ874   Sarfraz, Khurshid   I8767890    Leo -1
AZ874   Sarfraz, Khurshid   S5654678    Canis   -1
AZ874   Vijay, Garg     K7686546    Capricornus -1
AZ874   Zheng, Wang     J8767765    Eridanus    -1
AZ874   Zhuoran, Zhao   E6577779    Crux    -1
AZ900   Barnett, Arnold U7678765    Corvus  -1
AZ900   Berndt, Ernst   G5643211    Taurus  -1
AZ900   Carroll, John   G5656789    Cepheus -1
AZ900   Chen, Elaine    Y7876780    Draco   Nobel prize (Beauty)-1
AZ900   Chu, Zen    R2000098    Cepheus -1
AZ900   Costello, Anna  P7878665    Eridanus    -1
AZ900   Cusumano, Michael   E4543409    Eridanus    -1
AZ900   Dougherty, Jim  W1232121    Scorpius    -1
AZ900   Dyer, Barbara   T8888765    Crux    -1
AZ900   Eppinger, Steven    T2343111    Cancer  -1
AZ900   Fernandez, Roberto  L7077765    Leo -1
AZ900   Franck, Travis  C6755437    Canis   IBN chief researcher-1
AZ900   Vijay, Garg R6567123    Corona  -1
AZ900   Zheng, Wang U1321254    Capricornus -1

提前谢谢你。我一直在努力解决它。

1 个答案:

答案 0 :(得分:0)

这里有很多问题。

您已从行的开头(偏移0)对行进行排序。您正尝试从偏移量0排序数据中的偏移量6进行二分搜索。那永远不会奏效!如果您在偏移量6的行内进行二进制搜索,则应该对相同偏移量的行进行排序,因为如果从偏移量6排序,行的顺序将不同。

请看下面的代码块:

//These are STRINGS to get the first name from the flight900 list
String str0 = array[middle].substring(6,9);
String str1 = array[middle].substring(6,10);
String str2 = array[middle].substring(6,11);
String str3 = array[middle].substring(6,12);
String str4 = array[middle].substring(6,13);

看起来解析列表中的第一个名称是一种可怕的方法。仅仅因为名称的长度不同,您无法尝试创建所有可能的子串并进行比较!您必须使用适当的分隔符解析该行,以便您可以干净地分离出用于比较的名称。那么你就不会有那么多字符串要比较了!

建议:

解析该行并从中创建一个对象。您可以将数据放在自定义类中,甚至可以放在具有键值对的映射中。基本上你应该以解析的格式保存行数据,以便轻松使用内容。

然后,如果要按航班名称搜索,请创建一个地图,其中已解析的航班名称为关键,而已解析的对象为值。现在,要查找任何航班,您可以从地图中的名称中获取值。如果有多个具有相同名称的航班(可能不是这种情况,但我不知道您的数据),那么您应该针对每个航班名称保留一个列表,以保存从每个航线分析的多个航班数据对象。当您查找航班名称时,您将获得与该名称匹配的所有航班的列表。

希望有所帮助!