简体   繁体   中英

Java Palindrome program (1 error)

I asked a question yesterday about palindromes and Java:

Java Palindrome Program (am I on track)?

I've made some progress so far with all your help (thank you very much again). I just need help with one more thing before I can test the code out. I'm using Eclipse and I'm getting an error on one line (I'll also include the error as a comment in the code below). I keep getting a "Cannot invoke charAt(int) on the array type String[]" .

Anyone know what is going on here? It's been a while since I used Java. Used it in CS One about 12 months ago, then I moved on to C++ in Data Structures, then Machine Code and Assembly Language in the next course. Here's the code (I've also included the error in a comment in the code). Thanks a lot:

public class Palindrome 
{

public boolean isPalindrome( String theWord )  
{    
    for ( int i = 0; i < theWord.length( ); i++ ) {
        if ( theWord.charAt(i) != theWord.charAt (theWord.length() - i - 1) ) {
            return false;
        }
    }
    return true;
}


public static void main( String [] theWord ) 
{
        int leftPointer = 0;
        int rightPointer = theWord.length - 1;

        for ( int i = 0; i < theWord.length / 2; i++ ) {
            while (leftPointer >= rightPointer) {
                if ( theWord.charAt(i) == theWord.charAt (theWord.length - i - 1) ) { // Error: Cannot invoke charAt(int) on the array type String[]
                    leftPointer++;
                    rightPointer--;
                }
                System.out.println(theWord);
            }
        }
}

}

You are trying to access a charAt() on an String[] (A String array of the arguments passed to your program), but you need to access it on a String. I world suggest something like:

if ( theWord[i].charAt(0) == theWord[theWord.length - i - 1].charAt (0) ) {

That might help you.

charAt(int index) is applied for a String, not a String array. Your program want to decide whether a string is a palindrome, like "abcba". Instead of check whether an array of Strings are all palindrome, right? For example {"abcba", "bbc", "aba"}.

您在调用方法 .length() 后忘记了 ()

In Java (as in C++) the program received parameter list, which is the array of Strings. Thus your class should looks like below:

public class Palindrome 
{
  public static boolean isPalindrome( String theWord )  
  {    
    for ( int i = 0; i < theWord.length( ); i++ ) {
      if ( theWord.charAt(i) != theWord.charAt (theWord.length() - i - 1) ) {
        return false;
      }
    }
    return true;
  }


  public static void main( String [] args ) 
  {
    String theWord = args[0];  // first word passed to the program
    boolean isPalindrom = Palindrome.isPalindrome(theWord);
    System.out.println(theWord + " is" + ( isPalindrom ? "" : " NOT " )   + " a Palindrome." ); 
  }

}
public static boolean isPalindrom(String value) {
    if (value == null || value.length()==0 || value.length()==1) {
        return true;
    }
    if(value.charAt(0)!=value.charAt(value.length()-1)) {
        return false;
    }
    StringBuilder newValue =new StringBuilder(value);
    newValue = new StringBuilder(newValue.substring(1, newValue.length()));
    newValue = new StringBuilder(newValue.substring(0, newValue.length()-1));
    return isPalindrom(newValue.toString());
}

try this simple recursive method.

import java.util.*;
public class MyClass {
    public static void main(String args[]) {
      
      String str = "pappap";
      String sp = str.toLowerCase();
      char[] ch = sp.toCharArray();
      int len = ch.length;
      int lastIndex = ch.length-1;
      int count = 1;
      int first = 0;
      int last = sp.length()-1;
     for(; first < last  ;){  
              if(ch[first] == ch[last] ){
                  first= first+1;
                  last= last-1; 
               }
             else{
              count = 0;
              break;
              }
      }
      
    String result = (count == 1) ?  "Palindrome" : "Not a palindrome " ;
    System.out.println(result);
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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