[英]Program to check for a Palindrome
I'm trying to write a program that converts a long into a string and checks if it is a palindrome, I've written the following so far, but it gives me an incompatible types error, and I can't seem to find what's causing it. 我正在尝试编写一个将long转换为字符串并检查它是否是回文式的程序,到目前为止,我已经编写了以下内容,但是它给了我一个不兼容的类型错误,而且我似乎找不到什么造成它。 :S Any help would be much appreciated :) :S任何帮助将不胜感激:)
The error occurs at line 24 and it says incompatible types - found void but expected java.lang.String 该错误发生在第24行,并指出类型不兼容-发现无效,但应为java.lang.String
public class programPalindrome
{
private String go()
{
Input in = new Input ();
System.out.print("Enter Number: ");
return in.nextLine();
long number = in.nextLong();
String Palindrome = Long.toString(number); // converts the long into a string
String newAnswer = reverse(Palindrome);
String anotherAnswer = reverseCheck(Palindrome,newAnswer);
System.out.println("This is a Palindrome" + Palindrome);
}
// Check to see if the two argument Strings are the reverse of each
// other.
private String reverseCheck(String Palindrome, String newAnswer)
{
if (Palindrome.compareTo(newAnswer) == 0) {
return System.out.println("It is a palindrome");
}
else
{
return System.out.println("It is not a palindrome");
}
}
// Return a String which is the reverse of the argument String
private String reverse(final String Palindrome)
{
String result = new String();
int position = 0;
while (position < Palindrome.length())
{
result = new Character(newAnswer.charAt(position)).toString() + result;
position = position + 1;
}
return result;
}
public static void main(String[] args)
{
new programPalindrome().go();
}
}
Try changing 尝试改变
return System.out.println("It is not a palindrome");
to 至
return "It is not a palindrome";
and 和
return System.out.println("It is a palindrome");
to 至
return "It is a palindrome";
(Added - Additionally, there's a logic error in your go()
method. It prints out "This is a palindrome" regardless of whether it works or not...) (添加-另外,您的go()
方法中存在逻辑错误。无论是否工作,它都会打印出“这是回文”)。
I made it something like this, below. 我在下面做了这样的事情。 First, input from the user, then reverse of the string. 首先,从用户输入,然后反向输入字符串。 Next compare user string and reversed one, if is the same -> Print "Palindrom", if not "Not Palindrom". 接下来比较用户字符串并反转一个,如果相同->如果不是“ Not Palindrom”,则打印“ Palindrom”。 Added "ignoreCase" just in case. 添加“ ignoreCase”以防万一。 ;) ;)
public static void main(String[] args)
{
System.out.print("Enter the word: ");
Scanner userInput = new Scanner(System.in);
String word = userInput.nextLine();
String drow = new StringBuilder(word).reverse().toString();
if(word.equalsIgnoreCase(drow))
{
System.out.print("Palindrom");
}
else if (!word.equalsIgnoreCase(drow))
{
System.out.print("Not Palindrom");
}
}
Well, for one thing, you're calling System.out.println()
which returns void
, and attempting to return that as a String
in your reverseCheck() function. 好吧,一方面,您正在调用System.out.println()
并返回void
,并尝试在reverseCheck()函数中将其作为String
返回。 Decide: do you want to print the result, or return it (perhaps as a bool
)? 决定:您要打印结果还是将其返回(也许是bool
)?
you can't return System.out.println when your function signature says you want to return a String... you have some another syntax error on the line 当您的函数签名说您想返回一个字符串时,您将无法返回System.out.println。
result = new Character(newAnswer...
newAnswer can't be resolved in that scope... are you using an IDE like Eclipse? newAnswer在该范围内无法解决...您是否正在使用像Eclipse这样的IDE? That would probably help you out a lot. 那可能会对您有很大帮助。
Here's a more straightforward program to do the same thing: 这是执行相同操作的更简单的程序:
public class programPalindrome
{
static public boolean isPalindromic(long value){
String valueAsString = Long.toString(value);
String reverseString = (new StringBuffer(valueAsString)).reverse().toString();
if(valueAsString.equals(reverseString)){
return true;
}
else{
return false;
}
}
public static void main(String[] args)
{
System.out.println(args[0] + " is palindromic == "
+ isPalindromic(Long.parseLong(args[0])));
}
}
private String go()
{
Input in = new Input ();
System.out.print("Enter Number: ");
return in.nextLine();
long number = in.nextLong();
String Palindrome = Long.toString(number); // converts the long into a string
String newAnswer = reverse(Palindrome);
String anotherAnswer = reverseCheck(Palindrome,newAnswer);
System.out.println("This is a Palindrome" + Palindrome);
}
The compiler shouldn't let you get away with putting unreachable code after the return. 编译器不应该让您在返回之后放置无法访问的代码。 I don't think you understand how functions work. 我认为您不了解功能是如何工作的。
In your reverseCheck() method, you're trying to return a void but you have the return type listed as String. 在您的reverseCheck()方法中,您尝试返回一个void,但是您将返回类型列为String。 System.out.println() returns void - it just prints to the screen. System.out.println()返回void-只是打印到屏幕上。 Rather, return the String "it is a palindrome" or "it is not a palindrome" 而是返回字符串“这是回文”或“它不是回文”
return System.out.println("It is not a palindrome");
println不返回任何内容。
StringBuffer has a reverse method so this should work: StringBuffer有一个反向方法,因此可以使用:
public static boolean checkForPalindrom(String s){
StringBuffer buf=new StringBuffer(s);
return s.equals(buf.reverse().toString());
}
This is not explaining what is wrong with your code (it's already explained by others), but why don't you try to use this instead: 这并不是在解释您的代码有什么问题(其他人已经对此进行了解释),但是您为什么不尝试使用它呢?
public class SO {
public static void main(String[] args) {
System.out.println(isPalindrome("aaaaaa"));
System.out.println(isPalindrome("aaazzaa"));
System.out.println(isPalindrome("aaazaaa"));
System.out.println(isPalindrome("zzzbb"));
System.out.println(isPalindrome("zzbb"));
}
public static boolean isPalindrome(String word) {
for (int i = 0; i <= word.length()/2; i++) {
if (word.charAt(i) != word.charAt(word.length()-1-i)) {
return false;
}
}
return true;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.