String Palindrome计划

时间:2013-08-18 16:32:41

标签: java palindrome

我创建了一个字符串回文程序,但我得到了一个N​​ullpointer异常。

这是我写的代码:

public class JavaApplication2 {

    /**
     * @param args the command line arguments
     */
    char c[]={'i','c','i','c','i'};
    String s = new String(c);
    String s1=null;
    int i,j;
    char c1[];
    public static void main(String[] args) {
        //TODO code application logichere

        JavaApplication2 ja=new JavaApplication2();
        ja.palindrome();
        ja.ans();
        boolean b1= ja.ans();
        System.out.println(b1);
    }

    public void palindrome()
    {
        for(i=s.length()-1,j=0;i>=0;i--)
        {
            c1[j]=s.charAt(i);
            j++;
        }
    }

    public boolean ans()
    {
        String s2= new String(c1);
        if(s2.equals(s))
        {
            return true;
        }
        else
            return false;

    }
}

我无法弄清楚如何处理它以及为什么会发生这种错误。

5 个答案:

答案 0 :(得分:0)

问题是char c1[];未被初始化。因此,这一行给出了一个nullpointer:

String s2 = new String(c1);

对变量进行了初始化:

char c1[] = {};

现在NullPointer已经消失,但那不是最终版本,你需要用你的逻辑填充它。

答案 1 :(得分:0)

这个代码可以正常工作。你可以试试这个。

public class JavaApplication2 {
char c[]={'i','c','i','c','i'};
String s = new String(c);
int i,j;
char c1[]=new char[s.length()] ; // initialize c1
public static void main(String[] args) {
    JavaApplication2 ja=new JavaApplication2();
    ja.palindrome();
    ja.ans();
    boolean b1= ja.ans();
    System.out.println(b1);
}

public void palindrome()
{
    for(i=s.length()-1,j=0;i>=0;i--)
    {
        c1[j]=s.charAt(i);// populate c1
        j++;
    }
}

public boolean ans()
{
    String s2= new String(c1);
    if(s2.equals(s))// check equality of s2 and s
    {
        return true;
    }
    else
        return false;

}
}

答案 2 :(得分:0)

new StringBuilder(str1).reverser()。toString();

答案 3 :(得分:0)

只需从最后一个索引到第一个索引遍历,就可以轻松地反转字符或整数数组。

int[] myArray = new int[10];//Array size is 10
//Traverse from last index= 9 to the first Index= 0 
for(int i= myArray.length-1; i>= 0 ; --i){
    System.out.print(myArray[i]);//here i is last index=10-1=9.
}

但我试图通过忽略案例来检查用户给定的字符串是Palindrome,而不是忽略大小写。

import java.util.Scanner;

public class Palindrome {

    public void StringPalindrome(String original) {
        String reverse = "";
        for (int i = original.length() - 1; i >= 0; i--) {
            reverse += original.charAt(i);
        }
        if (original.equals(reverse)) {
            System.out.println("Entered string is palindrome \n" + "Original = " + original + "\n Reverse = " + reverse);
        } else {
            System.out.println("Entered string is not a palindrome.");
        }
    }

    public void StringPalindromeIgnoreCase(String original) {
        String reverse = "";
        for (int i = original.length() - 1; i >= 0; i--) {
            reverse += original.charAt(i);
        }
        if (original.equalsIgnoreCase(reverse)) {
            System.out.println("Entered string is palindrome \n" + "Original = " + original + "\n Reverse = " + reverse);
        } else {
            System.out.println("Entered string is not a palindrome.");
        }
    }

    public void reverseArrayInteger(int number[]) {
        for (int i = number.length - 1; i >= 0; i--) {
            System.out.print(number[i]);
        }
        System.out.println(" ");
    }

    public static void main(String[] args) {
        Palindrome myPalindrom = new Palindrome();
        testReverseIntegerArray(myPalindrom);
        testStringPalindrome(myPalindrom);
        testStringPaligrameIgnoreCase(myPalindrom);
    }

    public static void testReverseIntegerArray(Palindrome myPalindrom) {
        int[] number = {1, 3, 5, 7, 8};
        myPalindrom.reverseArrayInteger(number);
    }

    public static void testStringPalindrome(Palindrome myPalindrom) {
        Scanner in = new Scanner(System.in);
        String original;
        System.out.println("Enter a string to check if it is a palindrome");
        original = in.nextLine();
        myPalindrom.StringPalindrome(original);
    }

    public static void testStringPaligrameIgnoreCase(Palindrome myPalindrom) {
        Scanner in = new Scanner(System.in);
        String original;
        System.out.println("Enter a string to check if it is a palindrome");
        original = in.nextLine();
        myPalindrom.StringPalindromeIgnoreCase(original);
    }
}

简单输出:

87531 
Enter a string to check if it is a palindrome
MADAM
Entered string is palindrome 
Original = MADAM
 Reverse = MADAM
Enter a string to check if it is a palindrome
MaDAm
Entered string is palindrome 
Original = MaDAm
Reverse = mADaM

答案 4 :(得分:0)

这是我的代码:

class MyClass

{


public static void main (String[] args)


    {
        Scanner sc=new Scanner(System.in);

            int T=sc.nextInt();


            for(int j=0;j<T;j++){
            String str=sc.next();

            String revstring="";

            for(int i=str.length()-1;i>=0;--i){

            revstring +=str.charAt(i);
            }

            if(revstring.equalsIgnoreCase(str)){
            System.out.println("Yes");
            }
            else{
            System.out.println("No");
            }
        }
    }
}