我试图编码回文。它有一个用户输入字符串,程序将告诉它是否是回文。我在网上搜索了代码,但似乎当我创建了另一个方法时,编译时编译器会说“Nullpointerexception”。我从昨天开始尝试这个,但似乎我无法理解它。我需要它用于我们的ICT课程。谢谢。这是我的代码。
import java.util.Scanner;
public class Palindrome {
String word, reverse="";
public static void main(String args[]){
String word;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
word = in.nextLine();
Palindrome check = new Palindrome();
check.palindromeChecker();
}
public String palindromeChecker(){
int length = word.length();
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + word.charAt(i);
if (word.equals(reverse))
return "Palindrome";
else
return "Not a Palindrome";
}
}
答案 0 :(得分:4)
静态main方法中的变量“word”隐藏了Palindrome
类的实例变量“word”。
因此,您要将Scanner中的值分配给错误的变量。
要修复此错误,您有以下几种选择:
简单的方法是删除main方法中的变量,并将类Palindrome
的实例变量重新声明为静态。然而,这不是很优雅(而不是面向对象)。
更好的方法是将您读取的值作为参数传递给方法palindromeChecker
。
当然还有很多其他方法可以做到这一点,但我相信一旦你对这门语言和编程有了更深入的了解,你会学到更多的方法。
答案 1 :(得分:1)
这足以检查回文
String str="abccba";
String newStr=new StringBuilder(str).reverse().toString();
if(str.equals(newStr)){
System.out.println("is palindrome");
} else{
System.out.println("not a palindrome");
}
但是你仍然想以艰难的方式改变你的代码,如下所示
public static void main(String[] args) throws ParseException {
String word;
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
word = in.nextLine();
System.out.println(palindromeChecker(word));
}
public static String palindromeChecker(String word){
int length = word.length();
String reverse="";
for ( int i = length - 1 ; i >= 0 ; i-- )
reverse = reverse + word.charAt(i);
if (word.equals(reverse))
return "Palindrome";
else
return "Not a Palindrome";
}
答案 2 :(得分:1)
您可以删除main方法中的局部变量'word',并将全局变量'word'和'reverse'更改为static。但最好在palindromeChecker方法中传递一个String参数
答案 3 :(得分:0)
{ Scanner s=new Scanner(System.in);
System.out.println("enter the string:");
String str=s.nextLine().toString();
String newStr=new StringBuilder(str).reverse().toString();
if(str.equals(newStr)){
System.out.println("is palindrome");
} else{
System.out.println("not a palindrome");
} }
}
答案 4 :(得分:0)
您正在尝试访问回文检查器中的非静态String
对象。这就是为什么它显示NULLPointerException
。
您应该尝试以下方式:
public class Palindrome { static String word, reverse=""; public static void main(String args[]) { Scanner in = new Scanner(System.in); System.out.println("Enter a string to check if it is a palindrome"); word = in.nextLine(); Palindrome check = new Palindrome(); System.out.println(check.palindromeChecker()); } public String palindromeChecker() { String reverse=""; int length = word.length(); for (int i = length - 1; i >= 0; i--) reverse = reverse + word.charAt(i); if (word.equals(reverse)) return "Palindrome"; else return "Not a Palindrome"; } }
答案 5 :(得分:0)
package ispolidrom;
import java.util.Scanner;
public class IsPolidrom {
public static void main(String[] args) {
int number;
Scanner input= new Scanner(System.in);
System.out.print("\n Enter number: ");
number=input.nextInt();
if(isPolidrome(number)) {
System.out.printf("\n %d is polidrome",number);
}
else {
System.out.printf("\n %d is NOT polidrome",number);
}
}
public static boolean isPolidrome(int myNum) {
String numStr;
numStr=Integer.toString(myNum);
String revNum;
StringBuffer buffer= new StringBuffer(numStr);
revNum=buffer.reverse().toString();
if(numStr.equals(revNum)) {
return true;
}
else {
return false;
}
}
}
答案 6 :(得分:0)
除了修复变量范围问题。我认为使用以下代码可以提高算法效率,
public class Palindrome {
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
String word = keyboard.nextLine();
if(isPalindrome(word))
System.out.println(word + " is a Palindrome");
else
System.out.println(word + " is not a Palindrome" );
}
public static boolean isPalindrome(String word){
int length = word.length();
//loop until i reaches the midpoint index
for(int i = 0; i < length/2 ; i++){
//compare first char with last, second with second last
//and so on until it reaches the mid point
if(word.charAt(i) != word.charAt(length-i-1))
return false;
}
return true;
}
}
答案 7 :(得分:0)
public class Palindrome
{
static String word, reverse="";
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("Enter a string to check if it is a palindrome");
word = in.nextLine();
Palindrome check = new Palindrome();
System.out.println(check.palindromeChecker());
}
public String palindromeChecker()
{
String reverse="";
int length = word.length();
for (int i = length - 1; i >= 0; i--)
reverse = reverse + word.charAt(i);
if (word.equals(reverse))
return "Palindrome";
else
return "Not a Palindrome";
}
}