简体   繁体   English

string.length在java中无法正常工作

[英]string.length not working properly in java

Hi I have a code that checks if a string is palindrome or not.the code is like this: 嗨,我有一个代码,检查字符串是否是回文。代码是这样的:

         package ProjeTarahi;

         import java.util.*;
         import java.io.File;
         import java.io.FileInputStream;
         import java.io.FileNotFoundException;
         import java.util.Scanner;
         import java.util.logging.Level;
         import java.util.logging.Logger;
         import java.lang.String;

         public class Main 
         {

         public boolean CheckIsSymmetric(String s)
            {
               if (s.length()<=1) 
               {
                   return true;

                }

        if (s.charAt(0)==s.charAt(s.length()-1))
        {
            String sub = s.substring(1,s.length()-2);
            return CheckIsSymmetric(sub);
        }
        else
        {
            return false;
        }


    }

    public static void main(String args[])throws FileNotFoundException
    {
        Scanner sc=new Scanner(new FileInputStream(new File("in.txt")));
        String input=sc.nextLine();
        Main p=new Main();

        if(p.CheckIsSymmetric(input)==true)
        {
            System.out.println("in reshte motegharen ast");
        }
        else 
        {
            System.out.println("infinite");
        }

    }
}

I have written a code in c# that is exactly the same as the code above and its working very well but its not working properly in java and its output is always infinite. 我在c#中编写了一个与上面的代码完全相同的代码,它的工作非常好,但它在java中不能正常工作,它的输出总是无限的。 I stepped over my code and i think the problem is in the first if-statement of the CheckSymmetric() and it always jumps it but i don't know why.anybody can help me plz? 我跨过了我的代码,我认为问题出现在CheckSymmetric()的第一个if语句中,它总是跳过它,但我不知道为什么。任何人都可以帮助我PLZ?

This is a difference between String.Substring(int) in .NET, and String.substring(int, int) in Java. 这是.NET中的String.Substring(int)和Java中的String.substring(int, int)之间的区别。

In .NET, the second parameter is the length of the substring you're trying to take. 在.NET中,第二个参数是您尝试采用的子字符串的长度

In Java, the second parameter is the exclusive end index of the substring you're trying to take. 在Java中,第二个参数是您尝试采用的子字符串的独占结束索引

For example: 例如:

// .NET
"Hello world".Substring(3, 4) => "lo w"

// Java
"Hello world".substring(3, 4) => "w"

You're trying to take a substring which ends 1 character before the end of the string, so you want 您正在尝试获取一个子字符串,该子字符串在字符串结尾之前结束1个字符,因此您需要

String sub = s.substring(1, s.length() - 1);

Lessons to take from this: 从中吸取教训:

  • Assume your code is wrong before you assume that such a fundamental part of the platform is wrong. 在假设平台的这个基本部分是错误的之前,假设您的代码是错误的。 The chances of you discovering a bug in something as simple and fundamental as String.length() are virtually zero. 你发现像String.length()那样简单和基本的东西的bug的几率几乎为零。
  • Use debugging and diagnostics to work out where the problem is: you should have been able to see that the Java code was trimming too much off the end of the string. 使用调试和诊断来解决问题所在:您应该能够看到Java代码在字符串末尾修剪过多。
  • Don't assume that two methods with the same signature on different platforms will work exactly the same way. 不要假设在不同平台上具有相同签名的两种方法将以完全相同的方式工作。 Read the documentation! 阅读文档!

length method in String returns the length of the string. String中的length方法返回字符串的长度。 Here you want to get the substring after remove first char and last char. 在这里,您希望在删除第一个char和最后一个char后获取子字符串。 Thus 从而

    String sub = s.substring(1,s.length()-2);

should be: 应该:

    String sub = s.substring(1,s.length()-1);

Reading the Java documentation on substring , I noticed this line: 阅读子字符串上Java文档 ,我注意到这一行:

The substring begins at the specified beginIndex and extends to the character at index endIndex - 1 子字符串从指定的beginIndex开始,并扩展到索引endIndex - 1处的字符

So to get your desired result, you'll want to change it to 因此,要获得所需的结果,您需要将其更改为

 String sub = s.substring(1, s.length() - 1);

Java doc for String.substring: String.substring的Java文档:
* @param beginIndex the beginning index, inclusive. * @param beginIndex起始索引,包括在内。
* @param endIndex the ending index, exclusive. * @param endIndex结束索引,独占。

It means that: String s = "AbccbA"; 它意味着:String s =“AbccbA”; System.out.println(s.substring(1,s.length()-2)); 的System.out.println(s.substring(1,s.length() - 2));

returns: "bcc" but not "bccb" as you expected. 返回:“bcc”但不是你所期望的“bccb”。

public boolean checkIsSymmetric(String s) {
  return new StringBuilder(s).reverse().toString().equals(s);
}

Keep it simple and use the API. 保持简单并使用API​​。

The only issue with your code is that you have to change: 您的代码唯一的问题是您必须更改:

String sub = s.substring(1,s.length()-2); to
String sub = s.substring(1,s.length()-1);

Your code won't give you the desired result and also you will be getting a IndexOutOfBoundsException when your input is of length 2. 您的代码不会提供所需的结果,并且当您的输入长度为2时,您将获得IndexOutOfBoundsException

I guess your issue may be that you are reading the string from file, and at the end of the file the editor might be adding some extra character with is not visible to the eye. 我想你的问题可能是你正在从文件中读取字符串,并且在文件的末尾,编辑器可能会添加一些额外的字符,这是不可见的。

Insted of reading from a file try to hardcode and check your code 从文件中读取的内容尝试硬编码并检查您的代码

//String input=sc.nextLine();
String input = "d";

I guess this should work. 我想这应该有效。

Surprisingly this type of question appear a lot in Computer-Science related classes. 令人惊讶的是,这类问题在计算机科学相关课程中出现了很多。 Here is my version of finding palindrome of a String. 这是我查找字符串回文的版本。 This version is design to strip away any non-word characters and reduce every character to lower case. 此版本旨在剥离任何非单词字符,并将每个字符缩小为小写。 It also work with numbers as it will just compare it as a String. 它也适用于数字,因为它只是将它作为一个字符串进行比较。 It's only 13 lines long with a main and a Boolean method. 它只有13行,主要和布尔方法。 No for loop, substring or anything. 不用于循环,子串或任何东西。

Hope I help! 希望我帮忙! -Cheers -干杯

p/s: I apologize for my style of indentation :D p / s:我为自己的缩进风格道歉:D

import java.util.Scanner;
public class Palindrome  {
   public static void main(String[]args){
      if(isReverse()){System.out.println("This is a palindrome.");}
      else{System.out.print("This is not a palindrome");}
   }
   public static boolean isReverse(){
     Scanner keyboard =  new Scanner(System.in);
      System.out.print("Please type something: "); 
      String line = ((keyboard.nextLine()).toLowerCase()).replaceAll("\\W","");
      return (line.equals(new StringBuffer(line).reverse().toString()));
   }
}

I used the StringBuffer integrated class of Java and the meta character "/W" (all non-word characters, must be capital W). 我使用了String的StringBuffer集成类和元字符“/ W”(所有非单词字符,必须是大写字母W)。 Please feel free to criticize or discuss! 请随意批评或讨论!

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

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