简体   繁体   English

异常字符串比较Java

[英]Unusual String Comparison Java

I have been reading the previous string comparison questions here, and all of them are using an approach that is different from the one I am using. 我在这里阅读了以前的字符串比较问题,并且所有问题都使用与我正在使用的方法不同的方法。 Perhaps their approach is much better, but I want to get this using my approach. 也许他们的方法要好得多,但是我想用我的方法来解决。 I'm trying to write a function that compares two strings and outputs the one that is alphabetically first where capital letters are always considered to precede lower case letters. 我正在尝试编写一个比较两个字符串并输出按字母顺序排列的字符串的函数,在该函数中大写字母始终被认为在小写字母之前。 Java's compareTo method won't work because it ignores the letter case when alphabetizing. Java的compareTo方法将不起作用,因为它在按字母顺序排列时会忽略字母大小写。

I'm putting a for loop inside a while loop. 我将for循环放入while循环中。 When the for loop detects one letter of a string to be less than another letter in terms of ASCII value, it should exit the while loop and not complete the for loop, which is supposed to happen. 当for循环检测到一个字符串的一个字母小于ASCII值的另一个字母时,它应该退出while循环,并且不应该完成for循环。 However, the program should be returning "cdf", but it's returning "dbc" even though c precedes d in the alphabet. 但是,该程序应该返回“ cdf”,但是即使c在字母d之前也返回“ dbc”。 Why is the program returning 'dbc' and not 'cdf'? 为什么程序返回“ dbc”而不是“ cdf”?

public class Alphabet {
    public static String min_compare(String str1, String str2) {
        int a = 0;
        while (a == 0) {
            for (int i = 0; i < str1.length(); i++) {
                int b = (int) str1.charAt(i);
                int c = (int) str2.charAt(i);
                if (b < c) {
                    a = 1;
                } else if (b > c) {
                    a = 2;
                } else if ((b == c) && (i == (str1.length() - 1))) {
                    a = 1;
                } else {
                    a = 0;
                }
            }
        }
        if (a == 2) {
            return str2;
        } else {
            return str1;
        }
    }

    public static void main(String[] args) {
        String check = min_compare("dbc", "cdf");
        System.out.println(check);
    }
}
  1. There is no need for the while loop; 不需要while循环; if a is 0 after the for loop finishes, running that for loop again will not result in a different outcome, and thus cause an infinite loop. 如果在for循环完成后a为0,则再次运行该for循环不会导致不同的结果,从而导致无限循环。

  2. You set a for every character of str1 ; 您为str1 每个字符设置a ; thus, the final value of a will only be determined by the last iteration of the for loop, ignoring what came before. 因此,最终值a只会通过的最后一次迭代中确定for循环,无视之前什么来。

Java's compareTo method won't work because it ignores the letter case when alphabetizing. Java的compareTo方法将不起作用,因为它在按字母顺序排列时会忽略字母大小写。

That is not correct. 那是不对的。 The String.compareTo(String) is specified as ordering the string lexicographically , which the javadoc describes as follows: String.compareTo(String)被指定为按字典顺序对字符串进行排序, javadoc描述如下:

" This is the definition of lexicographic ordering. If two strings are different, then either they have different characters at some index that is a valid index for both strings, or their lengths are different, or both. If they have different characters at one or more index positions, let k be the smallest such index; then the string whose character at position k has the smaller value, as determined by using the < operator, lexicographically precedes the other string." 这是字典顺序的定义。如果两个字符串不同,则它们在某个索引处具有不同的字符(这是两个字符串的有效索引),或者它们的长度不同,或者两者都不同。索引位置越多,则k是此类索引中的最小值;然后,在字符串位置k处具有较小值(通过使用<运算符确定)的字符串,按字典顺序在另一个字符串之前。”

If you read that carefully, you will see that it does not ignore case. 如果仔细阅读,您会发现它不会忽略大小写。 Also, if you look at the Unicode code charts, you will see that capital letters precede the corresponding lowercase letters ... at least for non-accented letters in the Latin alphabet. 另外,如果您查看Unicode代码表,您将看到大写字母位于相应的小写字母之前……至少对于拉丁字母中的非重音字母而言。

Therefore the compareTo method will order strings according to what you need, provided that you have stated your requirements correctly in the Question. 因此,前提是您在“问题”中正确说明了您的要求,然后compareTo方法将根据您的需要对字符串进行排序。


Looking at your method, I notice that it doesn't implement the standard int compare(String, String) signature. 查看您的方法,我注意到它没有实现标准的int compare(String, String)签名。 So you can't directly replace it with compareTo. 因此,您不能直接用compareTo替换它。

I can also see a bug: If str2 is a prefix of str1 , and str1 is longer, then you will get an exception. 我还可以看到一个错误:如果str2str1的前缀,而str1更长,那么您将得到一个异常。

In fact, your method could be rewritten (correctly) as: 实际上,您的方法可以重写为(正确)为:

  public static String minCompare(String str1, String str2) {
      return str1.compareTo(str2) > 0 ? str2 : str1;
  }

You mentioned 你提到

it should exit the while loop and not complete the for loop 它应该退出while循环,而不是完成for循环

However, you haven't implemented it in your code. 但是,您尚未在代码中实现它。 I have added breaking nested loop code. 我添加了中断嵌套循环代码。 Hope it will help. 希望它会有所帮助。 Thx. 谢谢。

public class Alphabet {

    public static String min_compare(String str1, String str2) {

        int a = 0;

        whileloop:
        while (a == 0) {
            for (int i = 0; i < str1.length(); i++) {

                int b = (int) str1.charAt(i);
                int c = (int) str2.charAt(i);
                if (b < c) {
                    a = 1;
                    break whileloop;
                } else if (b > c) {
                    a = 2;
                    break whileloop;
                } else if ((b == c) && (i == (str1.length() - 1))) {
                    a = 1;
                    break whileloop;
                }

                else {

                    a = 0;

                }
            }

        }

        if (a == 2) {
            return str2;
        } else {
            return str1;
        }
    }

    public static void main(String[] args) {

        String check = min_compare("dbc", "cdf");
        System.out.println(check);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM