[英]Checking, if String is Palindrome using substring() and charAt()
我的代碼應該要求用戶輸入一個String,然后使用charAt()
和substring()
方法檢查它是否是Palindrome。 我相信,我已經做了一切正確但NetBeans正在編譯錯誤。
當我運行我的代碼時,NetBeans告訴我:
Exception in thread "main" java.lang.RuntimeException: Uncompilable source code - missing return statement
at palindrome.Palindrome.methodA(Palindrome.java:31)
at palindrome.Palindrome.main(Palindrome.java:27)
Java Result: 1
所以,基本上它說我錯過了一個返回聲明? 我不明白這是怎么可能的。
任何幫助將非常感謝伙計們。 謝謝。
package palindrome;
import java.util.Scanner;
public class Palindrome {
/**
* Program asks user to enter a string.
*/
public static void main(String[] args) {
String userInput; //user-inputted String
String result;
//set up instance of Scanner for user input
Scanner scan = new Scanner(System.in);
//ask user for input
System.out.print("Please enter a String: ");
userInput = scan.nextLine();
//run methodA
result = methodA(userInput);
}
public static String methodA(String inString) {
/*
* This method checks the user-inputted String against a backward copy
* of itself using only String and Character methods.
*/
//next two int variables used as int pointers for first letter of String
//and last letter of user-inputted String
int begPoint = 0;
int endPoint = inString.length() - 1;
//define Strings to return to main method
String isPal = (inString + " is a palindrome!");
String notPal = (inString + " is not a palindrome.");
while (begPoint < endPoint) {
//two substrings defined to test String character for character
String firstChar = inString.substring(begPoint, begPoint + 1);
String lastChar = inString.substring(endPoint, endPoint + 1);
//algorithm continues step by step with begPoint going up one and
//endPoint decreasing by one
begPoint++;
endPoint--;
//basically here I am trying to check the characters of the user-
//inputted Strings using the charAt methods but I'm lost when I
//get to this point
if (inString.charAt(begPoint) == inString.charAt(endPoint)) {
return isPal;
} else {
return notPal;
}
}
}
}
經過一些回答 - 我現在已將我的while語句更改為:
while(begPoint<endPoint) {
//two substrings defined to test String character for character
String firstChar = inString.substring(begPoint, begPoint + 1);
String lastChar = inString.substring(endPoint, endPoint + 1);
//basically here I am trying to check the characters of the user-
//inputted Strings using the charAt methods
if (inString.charAt(begPoint) == inString.charAt(endPoint)) {
return isPal;
} else {
return notPal;
}
//algorithm continues step by step with begPoint going up one and
//endPoint decreasing by one
begPoint++;
endPoint--;
}
但是,現在它告訴我(begPoint ++)是一個無法訪問的語句。
在這段代碼中
if(inString.charAt(begPoint)==inString.charAt(endPoint))
{
return isPal;
}
else
{
return notPal;
}
你基本上是在循環的第一次迭代中返回。
如果它不是Palindrome,那么立即返回是有意義的,但是其他方面你需要繼續循環。
如果沒有輸入while
,代碼也不會編譯。 也許返回notPal
另外(感謝ArtOfWarfare),你需要搬家
//algorithm continues step by step with begPoint going up one and
//endPoint decreasing by one
begPoint++;
endPoint--;
到你的while循環結束,即在完成if
語句之后。
你的問題是你在檢查charAts之前移動了begPoint和endPoint。 在使用方法charAt之后,將代碼前進begPoint和endPoint移動到。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.