An error program in the third edition of Java language guide

Source: Internet
Author: User

Don't scold me for coming in to the audience. You must think that I am a bad guy who hasn't made a teacher can question the master's book. I think the master should not make such a mistake. It should be the translator's incompetence or the boy's ignorance.

Let's talk about the program.

Original book procedure [see pages 97th and 98]

Public class Palindrome <br/>{< br/> public static boolean isPalindrome (String stringToTest) <br/>{< br/> String workingCopy = removeJunk (stringToTest ); <br/> String reversedCopy = reverse (workingCopy); <br/> return reversedCopy. equalsIgnoreCase (workingCopy); <br/>}< br/> protected static String removeJunk (String string) <br/>{< br/> int I, len = String. length (); <br/> StringBuffer dest = new StringBuffer (Len); <br/> char c; <br/> for (I = (len-1); I> = 0; I --) <br/>{< br/> c = string. charAt (I); <br/> if (Character. isLetterOrDigit (c) <br/>{< br/> dest. append (c); <br/>}< br/> return dest. toString (); <br/>}< br/> protected static String reverse (String string) <br/>{< br/> StringBuffer sb = new StringBuffer (string. length (); <br/> return sb. toString (); <br/>}< br/> public static void main (String [] agrs) <B R/>{< br/> String string = "Madam, I'm Adamsdf. "; <br/> System. out. println (); <br/> System. out. println ("test whether the following data is a Bounce Message"); <br/> System. out. println (string); <br/> if (isPalindrome (string) <br/>{< br/> System. out. println ("It is a palindrome! "); <Br/>}< br/> else <br/>{< br/> System. out. println (" It is not a palindrome! "); <Br/>}< br/>}

This is a program that determines whether a string is a return. After writing It, run It. For the existing string "Madam, I'm Adam", the output is indeed "It is a palindrome !", However, I tried to modify the string (not a text return), but the program results are still the same. Why am I depressed? I read N times and found that I copied exactly the same as what I wrote in the book. So I went back to check the program.

The removeJunk () method extracts each character from the end of a string and places it in a character buffer zone.

Now that we have obtained the inverted order, we should compare it with the positive order. OK? So I think the function of the reverseCopy () function is to reverse the string obtained above. So is it true? Let's take a look. The code for the reverse () function is as follows:

Protected static String reverse (String string) <br/>{< br/> StringBuffer sb = new StringBuffer (String. length (); <br/> return sb. reverse (). toString (); <br/>}

We can see that the program does not reverse the string order. It seems that the book is really wrong.

I changed the reverse () function to the following:

Protected static String reverse (String string) <br/>{< br/> StringBuffer sb = new StringBuffer (String. length (); <br/> for (int I = string. length ()-1; I> = 0; I --) <br/> sb. append (string. charAt (I); <br/> return sb. toString (); <br/>}

Test: It's successful. Well, it seems like this is the case.

 

The kid is ignorant and easy to understand. Do not blame the prawns. If you cannot find it, please give me some advice! Thank you!

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.