gpt4 book ai didi

java - java回文客户端程序错误"Cannot find symbol"

转载 作者:太空宇宙 更新时间:2023-11-04 07:05:15 25 4
gpt4 key购买 nike

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

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

最佳答案

我改变了一些东西来完成这项工作!

> 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

测试回文代码如下......

>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

关于java - java回文客户端程序错误"Cannot find symbol",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21520509/

25 4 0