Java Palindrome Program (am I on track)?

北城余情 提交于 2019-12-11 13:07:46

问题


I have only 6 months of Java experience (and I'm also new here) so please bear with me if things don't look entirely right in my code. Please note that it's still a work in progress. I'm trying to write a program that takes in strings and prints only the ones that are palindromes.

I'm supposed to: - create a method named isPalindrome, which has a String parameter and - returns a Boolean based on whether the string is a palindrome or not. Then - modify the main method to use isPalindrome to print only the palindromes.

For example, if I type: "madam James apple mom timer", it should print "madam" and "mom".

This is basically the program I am trying to write: Ex: Let's use the word "madam". The program will check if the first and last letters match ("madam"). If that is true, then it'll check the next letters, this time "a" and "a" ("madam). And so on and so forth.

This is the Java code I have so far:

public class Palindrome 
{
    private String theWord; //Error: The value of the field Palindrome.theWord is not used

    public boolean isPalindrome( String theWord ) {
        int firstPointer = 0;
        int secondPointer = theWord.length() - 1;

        for ( int i = 0; i < theWord.length( ); i++ ) {
            if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) { //Error: charAt cannot be resolved or is not a field
                return true;
            }
            return false;
        }
    }


    public static void main( String[] theWord ) {
        Palindrome = new Palindrome( ); //Error: Palindrome cannot be resolved to a variable

        for ( int i = 0; i < theWord.length; i++ ) {
            while (firstPointer < secondPointer) { //Error: "firstPointer" cannot be resolved to a variable. "secondPointer" cannot be resolved to a variable
                if ( theWord.charAt[0] == theWord.charAt (theWord.length() - 1) ) {  //Error: charAt cannot be resolved to a variable or is not a field. Cannot invoke length() on the array type String[]
                    firstPointer++; //Error: "firstPointer" cannot be resolved to a variable
                    secondPointer++; //Error: "secondPointer" cannot be resolved to a variable
                }
                System.out.println(theWord);
            }
        }
    }
}

Any bit of help knowing where I've gone wrong would be greatly appreciated. Please don't just give me the right code. I would like to figure this out. Thank you very much.

**EDIT: I've included the errors as comments in the code now. I'm using Eclipse by the way.


-->**EDIT 2: Okay guys. I've read most of your answers and have been able to correct most of my code so far (Thank you all so much so far). The only part I'm still having an issue with right now is this part:

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1) ) {
                    leftPointer++;
                    rightPointer--;

I'm now getting a "Cannot invoke charAt(int) on the array type String[]" and "Cannot invoke length() on the array type String[]". Those are the only two errors remaining, then I'll test the code out. I've been trying to resolve them for a while now but I'm still not entirely sure what those errors mean.

Eclipse is suggesting that I change theWord.charAt(i) to theWord.length which is not what I want. It is also suggesting I remove "( )" from length but I don't think that's right either.


回答1:


Looking at your isPalindrome method :

if ( theWord.charAt(0) == theWord.charAt (theWord.length() - 1) 

here you always compare the first character to the last character. In each iteration you should compare a different pair of characters, until you find a pair that doesn't match, or reach the middle of the word.

You should use the i variable of your loop :

if ( theWord.charAt(i) == theWord.charAt (theWord.length() - i - 1) 

And the return value should be the exact opposite. If you find a pair of characters that don't match, you return false. Only if the loop ends without returning false, you return true.




回答2:


Okay, let's break everything down into little sizable chunks.

  1. Input a string
  2. Parse the string, check if it is a palindrome.
  3. Print out the words in the string which were palindromes.

    public class Main {
    
    public static void main(String[] args) {
    
        Scanner scan = new Scanner(System.in);
        System.out.println("Enter a sentence: ");
    
        String sentence = scan.nextLine(); // 1.
    
        String[] words = sentence.split(" ");
    
        for (String word : words) {  // 3.
            if (isPalindrome(word)) {
                System.out.println(word);
            }
        }
    }
    
    /**
     * Check if the string is a palindrome.
     * @param string
     * @return True if string is palindrome.
     */
    public static boolean isPalindrome(String string) { // 2.
    
        for (int i = 0; i < string.length() / 2; i++) {
            if (string.charAt(i) != string.charAt(string.length() - i - 1)) {
                return false;
            }
        }
    
        return true;
    }}
    

Some explanation

The method/function isPalindrome is static because we are calling it from a static context, that is the main function. If you want to use it non-statically you would place it in a class and create a object from that class. The rest should be understandable. :-)




回答3:


A better isPalindrome method

The shortest way is probably just following the definition: If you reverse the string, and it's still the same, then it's a palindrome:

public static boolean isPalindrome(String input)
{
    String reverse = new StringBuilder(input).reverse().toString();
    return input.equalsIgnoreCase(reverse);
}

But if there is an educational goal (?), and iterators should be used for some reason then, imho it makes more sense to iterate from the outside towards the inside of the string.

public static boolean isPalindrome(String input)
{
  int length = input.length();
  for (int i = 0; i < length/2 ; i++)
  {
    if (input.charAt(i) != (input.charAt(length-1-i))) return false;
  }
  return true;
}

Phrase parsing

In your example you used the input of the main String[] parameter. Here is just some information in case you wanted to split it to words manually.

Equivalent to what you got now:

String[] words = phrase.split("\\s+");
for (String word : words) 
{
  // do stuff
}

The split method uses a delimiter to split a String into a String[]. The delimiter \\s is a regex that represents all kinds of whitespace (not only spaces, but also tabs, new-line characters, etc ...).

But it's not perfect (and neither is your way), there can still be commas, dots and other marks in the phrase. You could filter these characters in an iteration, using the Character.isLetterOrDigit method. Alternatively, you could just perform a replace(...) to remove comma's, points and other marks. Or you could use more complex regular expressions as well.

About your code

The first error message : "The value of the field is not used". The error message is caused by the global private field theWord, because it is never used. It's not used because you also have a parameter with the same name inside the method isPalindrom(String theWord). Whenever you reference theWord inside that method, it will always give advantage to method arguments before considering global variables.

It looks like you are stuck here with a design contradiction. What exactly is the class Palindrome ? There are 2 options:

  1. Is it supposed to be a toolbox like the Math class ? like boolean value = Palindrome.isPalindrome("madam");?
  2. Or is it supposed to be an Object that you instantiate using a constructor ? like boolean value = new Palindrome("madam").isPalindrome();

Option 1: a toolbox:

public class Palindrome 
{
  // removed the private field theWord

  // make this method static !!
  public static boolean isPalindrome( String theWord ) {
    ...
  }

  public static void main( String[] theWord ) {
    // remove the Palindrome object

    // inside the loop check use the static method 
    // which does not require an object.
    if ( Palindrome.isPalindrome(word))
    {
    }
  }
}

Option 2: an object

public class Palindrome 
{
  // keep the private field theWord
  private String theWord;

  public Palindrome(String theWord)
  {
    // set the value of the argument to the private field
    this.theWord = theWord;
  }

  // don't make this method static 
  // also you don't need the parameter any more.
  // it will now use the global field theWord instead of a parameter.
  public boolean isPalindrome() {
    ...
  }

public static void main( String[] theWord ) {
    // inside the loop check use an object
    Palindrome palindrome = new Palindrome(word);
    if ( palindrome.isPalindrome())
    {
    }
}

As for the errors about the firstPointer and secondPointer. You need to define and initialize those variables. I.e. put int firstPointer = 0; before the loop.




回答4:


In the loop check it out this way:

boolean isPalin = true;
for ( int i = 0; i < theWord.length( )/2; i++ ) { // loop goes till half since no need to check after that
     if ( !(theWord.charAt(i) == theWord.charAt (theWord.length() - 1 - i)) ) { // will check each letter with each end letter
         isPalin = false;
         break;
     }
}
return isPalin;

Another things to add -

1 -firstPointer secondPointer are local variables to isPalindrome

2 - When u have decalared theWord as global variable there doent seems a need to pass it. You can use it within the same class.

3 - theWord in main(String[] theWord) would require you to provide input as arguments, it better you go for console input at runtime.

4 - In main you should split each word and pass it to isPalindrome. In your code you are not calling isPalindrome to check anywhere.



来源:https://stackoverflow.com/questions/28393233/java-palindrome-program-am-i-on-track

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!