简体   繁体   English

比较Java中的两个字符串

[英]compare two strings in java

I am trying to compare to strings by using an algorithm as you see below My code does not work .. eclipse does not show any error before I run the code 我试图通过使用算法与字符串进行比较,如下所示,我的代码不起作用..eclipse在运行代码之前未显示任何错误

    public class MysteryClass {
    public static void mystery(String n) {
        String k= "alla";
            if (k.charAt(k.length())==n.charAt(n.length())) {
                System.out.println("palindrom");
            } else {
                System.out.println("not palindrom");

            }
        }

    public static void main(String[] args) {
        MysteryClass.mystery("alla");
    }
}

but we I run the code I get 但是我们运行我得到的代码

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 4
    at java.lang.String.charAt(String.java:658)
    at shapes.MysteryClass.mystery(MysteryClass.java:6)
    at shapes.MysteryClass.main(MysteryClass.java:15)

How to fix that?? 如何解决? thanks 谢谢

Two issues with your code:- 您的代码有两个问题:

1)By your logic, tigert is also palindrome but it is actually not.Palindrome is when the word is completely reversed and it is still the same.Currently,you are checking only the first and last character 1)按照您的逻辑, tigert也是回文,但实际上不是。回文是单词完全颠倒并且仍然相同时的词。当前,您仅在检查第一个和最后一个字符

2)String is internally held as a char array and arrays have indexing from 0 to length -1 2)字符串在内部保留为char array并且数组的索引从0到length -1

The error is caused by the statement if (k.charAt(k.length())==n.charAt(n.length())) Above k.length() returns 4. the statement then resolves to: 错误是由以下语句引起的,如果(k.charAt(k.length())==n.charAt(n.length()))k.length()以上返回4,则该语句解析为:

(k.charAt(4) 

This will through error because the function charAt counts from the index 0. So it counts the characters at index 0, 1, 2, 3 and you are asking it to fetch character at index 4 which does not exist. 因为函数charAt从索引0开始计数,所以这将通过错误进行。因此,它对索引0、1、2、3处的字符进行计数,并且您要求它从索引4中获取不存在的字符。 A suggested alternative is : (k.charAt(k.length() -1)==n.charAt(n.length()-1)) 一个建议的替代方法是: (k.charAt(k.length() -1)==n.charAt(n.length()-1))

Not sure what you want, but your StringIndexOutOfBoundsException is cause by access an array's index which is does not exist. 不确定您要什么,但是您的StringIndexOutOfBoundsException是由访问不存在的数组的索引引起的。

Take your String k = "alla" as example, k.length() will return 4 because "alla" have four characters, and you are accessing k.charAt(4) , the "alla" break into as follows: 以您的String k = "alla"为例, k.length()将返回4因为"alla"有四个字符,并且您正在访问k.charAt(4)"alla"分为以下几类:

a l l a  // string
0 1 2 3  // index

As you can see the last index of "alla" is 3 , that's why you get StringIndexOutOfBoundsException . 如您所见, "alla"的最后一个索引是3 ,这就是为什么要获取StringIndexOutOfBoundsException的原因。 But you can solve it using .length() -1 as follow: 但是您可以使用.length() -1来解决它,如下所示:

 if (k.charAt(k.length()-1)==n.charAt(n.length()-1)) {
            System.out.println("palindrom");
        } else {
            System.out.println("not palindrom");

Alternative: 替代方案:

If you want to compare two string , you can use .equals method too, as follow: 如果要比较两个string ,也可以使用.equals方法,如下所示:

 if (k.equals(n)) {
            System.out.println("palindrom");
        } else {
            System.out.println("not palindrom");

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

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