[英]Java Palindrome program (1 error)
I asked a question yesterday about palindromes and Java:我昨天问了一个关于回文和 Java 的问题:
Java Palindrome Program (am I on track)? Java 回文程序(我是否走上正轨)?
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).
我正在使用 Eclipse,但在一行中出现错误(我还将将该错误作为注释包含在下面的代码中)。 I keep getting a "Cannot invoke charAt(int) on the array type String[]" .
我不断收到“无法在数组类型 String[] 上调用 charAt(int)” 。
Anyone know what is going on here?有谁知道这里发生了什么? It's been a while since I used Java.
我已经有一段时间没有使用 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.
大约 12 个月前在 CS One 中使用它,然后我在数据结构中转到 C++,然后在下一门课程中转到机器代码和汇编语言。 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.您正在尝试访问 String[](传递给程序的参数的 String 数组)上的 charAt(),但您需要在 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. charAt(int index) 应用于字符串,而不是字符串数组。 Your program want to decide whether a string is a palindrome, like "abcba".
你的程序想要判断一个字符串是否是回文,比如“abcba”。 Instead of check whether an array of Strings are all palindrome, right?
而不是检查字符串数组是否都是回文,对吗? For example {"abcba", "bbc", "aba"}.
例如 {"abcba", "bbc", "aba"}。
您在调用方法 .length() 后忘记了 ()
In Java (as in C++) the program received parameter list, which is the array of Strings.在 Java 中(如在 C++ 中)程序接收参数列表,它是字符串数组。 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.