java palindrome客户端程序的“找不到符号”错误

时间:2014-02-03 05:43:17

标签: java compiler-errors client symbols palindrome

我尝试了所有内容并浏览了许多论坛帖子,但无法找到我的程序问题。我正在尝试使用客户端类中的程序中的方法,它一直告诉我它找不到它。如果有人能帮我搞清楚,那就太好了。这是程序和错误:

import javax.swing.JOptionPane;

public class Palindrome {

   private String palindrome;

 public Palindrome(){
 } // end constructor

public Palindrome(String newPalindrome){
       palindrome = newPalindrome;
 } // end Palindrome

 public static boolean isPalindrome(){

      String temp = "";
       boolean toBe = true; // Is a palindrome
boolean notToBe = false; // Is not a palindrome
        int low = 0; // First character
      int high = temp.length() - 1; // Last character

         while (low < high){
             if (temp.charAt(low) != temp.charAt(high))
                          return notToBe;

               low++;
                  high--;
            } // end while

  return toBe; 
 }// end isPalindrome

     public static String filter(){

String palindrome = "";
StringBuilder stringBuilder = new StringBuilder(); // Create a string builder


for (int i = 0; i < palindrome.length(); i++) { // Examine each char in the string to skip alphanumeric char
  if (Character.isLetterOrDigit(palindrome.charAt(i))) {
    stringBuilder.append(palindrome.charAt(i));
  } // end if
} // end for

// Return a new filtered string
return stringBuilder.toString();
} // end filter

 public String removeWhite(){

     String temp = "";

       temp = temp.replaceAll("\\s+","");

         return temp;
 } // end removeWhite

 public String lowerCase(){

           String temp = "";

           temp = temp.toLowerCase();

       return temp;
  } // end toLowerCase

 } // end the class

以下是客户端代码:

 import java.util.Scanner;
 import javax.swing.JOptionPane;

 public class TestPalindrome {

public static void main(String args[]){


       Palindrome pal = new Palindrome("") ;

   Scanner input = new Scanner(System.in);                  
        System.out.print("Enter a string: ");// Prompt the user to enter a string
  String temporary = (input.nextLine());
    String string = ("");

        temporary = pal;
        temporary.lowerCase();
        string.removeWhite();
        string.filter();
        string.isPalindrome();

        System.out.println(string);                    
   } // end main method

} // end the class

错误:

        TestPalindrome.java:30: error: incompatible types

        temporary = pal;
                    ^ 
        required: String

        found:    Palindrome
       TestPalindrome.java:31: error: cannot find symbol
         temporary.lowerCase();
                  ^
        symbol:   method lowerCase()
       location: variable temporary of type String
       TestPalindrome.java:32: error: cannot find symbol
        string.removeWhite();
              ^
       symbol:   method removeWhite()
       location: variable string of type String
       TestPalindrome.java:33: error: cannot find symbol
        string.filter();
              ^
       symbol:   method filter()
       location: variable string of type String
       TestPalindrome.java:34: error: cannot find symbol
        string.isPalindrome();
              ^
        symbol:   method isPalindrome()
        location: variable string of type String
        5 errors

4 个答案:

答案 0 :(得分:1)

我改变了一些事情来完成这项工作!

> public class Palindrome {
    private String palindrome;

    public Palindrome() {
    } // end constructor

    public Palindrome(String newPalindrome) {
        palindrome = newPalindrome;
    } // end Palindrome

    public static boolean isPalindrome(String string) {

    String temp = string;
        boolean toBe = true; // Is a palindrome
        boolean notToBe = false; // Is not a palindrome
        int low = 0; // First character
        int high = temp.length() - 1; // Last character


        while (low < high) {
            if (temp.charAt(low) != temp.charAt(high))
                return notToBe;

            low++;
            high--;
        } // end while
        return toBe;
    }// end isPalindrome

    public static String filter(String string) {

        String palindrome = string;
        StringBuilder stringBuilder = new StringBuilder(); // Create a string
                                                            // builder

        for (int i = 0; i < palindrome.length(); i++) { // Examine each char in
                                                        // the string to skip
                                                        // alphanumeric char
            if (Character.isLetterOrDigit(palindrome.charAt(i))) {
                stringBuilder.append(palindrome.charAt(i));
            } // end if
        } // end for

        // Return a new filtered string
        return stringBuilder.toString();
    } // end filter

    public String removeWhite(String string) {

        String temp = string;

        temp = temp.replaceAll("\\s+", "");

        return temp;
    } // end removeWhite

    public String lowerCase(String string) {

        String temp = string;

        temp = temp.toLowerCase();

        return temp;
    } // end toLowerCase

} // end the class

testpalindrome代码如下......

>package san.org.tst1;

import java.util.Scanner;

public class TestPalindrome {
    public static void main(String args[]){


           Palindrome pal = new Palindrome("") ;

       Scanner input = new Scanner(System.in);                  
            System.out.print("Enter a string: ");// Prompt the user to enter a string
      String temporary = (input.nextLine());
        String string = ("");


          //  temporary.toLowerCase();
            String testIsLowercase = pal.lowerCase(string);
            String removeWhitetest = pal.removeWhite(string);
            String filter = pal.filter(string);
            boolean testIspalindrome =  pal.isPalindrome(string);

            System.out.println(" removing lowercase : " + testIsLowercase);  
            System.out.println(" removing removeWhite : " + removeWhitetest);                
            System.out.println(" removing Filter : " + filter);         
            System.out.println("Is Palindrome : " + testIspalindrome);                    



       } // end main method

    } // end the class

答案 1 :(得分:0)

不知道您使用的是哪个版本的java,但没有String.lowerCase()方法。

Palindrome can not be castString

答案 2 :(得分:0)

您正尝试将Palindrome对象分配给String,这没有意义。请注意,您已将string声明为String,因此无法调用方法lowerCase()filter(),因为它们仅针对Palindrome个对象进行了定义。您可能希望将removeWhite()lowerCase()的结果分配给String

答案 3 :(得分:0)

Palindrome在这种情况下它不能直接转换为String。您需要覆盖toString方法并在Palindrome上调用它以使其成为字符串。

您可以直接调用字符串上的trimtoLowerCase函数。这些函数绑定到Palindrome类。 isPalindrome应该是非静态的。每个函数中的temppalindrome局部变量应直接修改palindrome成员变量。