7 Replies - 5974 Views - Last Post: 22 April 2012 - 04:48 AM Rate Topic: -----

#1 fatooma92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-February 12

palindromes with string methods

Posted 22 April 2012 - 03:24 AM

Ok, I've been trying to do this for a few days, But I'm not getting it. Our Prof. is not very good, and I have no one to ask how to do this, I really need help with this.


Write a program that has an array of 5 Strings, and determines which ones are
palindromes (letter-case does not matter) .
Your code will be written in one class Palindrome.java and has a main() method.

Please note the following:
1. Do not write all your code in the main(). The main should only initialize the array, and
print the palindrome words found. You can break your problem into smaller pieces
dealt with in separate methods. For example you might need a method that determines
whether a String is a palindrome or not (What would it take as a parameter? what
would it return?)
2. You will need to use String methods (API).


All I have done so far is this, which is not complieing

import java.util.*;
/**
 * Write a description of class Palindrome here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Palindrome
{
  public static void main(String [] args) {
     String [] palindrome = {“civic”, “level”, “rotator”};
    }
}


Thanks....

Is This A Good Question/Topic? 0
  • +

Replies To: palindromes with string methods

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2205
  • View blog
  • Posts: 5,239
  • Joined: 10-September 10

Re: palindromes with string methods

Posted 22 April 2012 - 03:38 AM

Don't despair, DIC is here!

Oh, my! A 13-line program that doesn't compile. That's disappointing and frustrating, but I like your approach of writing a little then testing. Hopefully, that will continue.

The reason your code won't compile is that you have improper quotes, not the text ones but the ones that can be either left or right (open and close?) quotes. If you're using a fancy editor, like Word or similar, be sure to save your source as plain text.

Good luck!
Was This Post Helpful? 0
  • +
  • -

#3 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: palindromes with string methods

Posted 22 April 2012 - 03:42 AM

GregBrannon already gave you the reason why the code does not compile, I am not going to repeat that.

As for the assignment, the assignment itself already gives you two useful hints, one is to use a method and the other is to use the Java String API.
So first, let's think about when a word is a palindrome. As far as I remember (and I suppose the linguistics did not change), if the reverse of a string (or word) is the same as the original word, it is a palindrome. For example:

//A palindrome
level //Original word
level //reversed word

//Not a palindrome
word //Original word
drow //reversed word



Now, the definition of a palindrome is explained, let's see how to tackle this. As the assignment says, we need a method. And obviously, that method should take a string as an input to determine whether it is a palindrome or not. Then, in pseudocode (not actual code):

boolean isPalindrome(String word)
	//Reverse the word
	String reverseWord = reverse(Word); //Search in the String API of Java to find a suitable method
	
	//Determine if the reverseWord is the same as word
	return word is equal to reverseWord;



Then, for our main method, we need to determine our array of strings if they are palindromes. In short, we have to loop, retrieve the word in that array and call the method to determine if the word was a palindrome. In pseudocode (not actual code):

for each String str in wordArray{
	if(isPalindrome(str))
		Print str is a palindrome
	else
		Print str is not a palindrome
}



Good luck ^^

This post has been edited by karabasf: 22 April 2012 - 03:43 AM

Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5388
  • View blog
  • Posts: 27,384
  • Joined: 10-May 07

Re: palindromes with string methods

Posted 22 April 2012 - 03:48 AM

** renamed title to be more descriptive **

Please don't name your topic "I need help" or "I'm stuck". Please give a descriptive title to what the problem is.
Was This Post Helpful? 0
  • +
  • -

#5 fatooma92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 19-February 12

Re: palindromes with string methods

Posted 22 April 2012 - 04:34 AM

How to do the main?
Write a program that has an array of 5 Strings, and determines which ones are
palindromes (letter-case does not matter) .
Your code will be written in one class Palindrome.java and has a main() method.
Please note the following:
1. Do not write all your code in the main(). The main should only initialize the array, and
print the palindrome words found. You can break your problem into smaller pieces
dealt with in separate methods. For example you might need a method that determines
whether a String is a palindrome or not (What would it take as a parameter? what
would it return?)

public class Palindrome
{
  public static void main(String [] args) {
     String[] s = new String[5];
(how to write this)

}
   
   public static String Reverse(String x) {
        String hold = "";
        char [] c = x.toCharArray();
        for (int i = c.length-1; i>0; i--)
        hold = hold +c[i];
        return hold;
    }
    public static boolean pallindromCheck(String n) {
        String x=Reverse(n);
        if (x==n)
            return true;
        else
            return false;
        }
    
       
    
}



thanks...
Was This Post Helpful? 0
  • +
  • -

#6 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1363
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: palindromes with string methods

Posted 22 April 2012 - 04:41 AM

So you loop through each element of the array, each time calling the palindromeCheck method and printing out the result-

for(//loop through each element in the array)
{
    if(palindDromeCheck(currentElement)
        // print result
    else
        // print result
}


Also you may find it useful to use an array initializer -

String[] s = {"word1", "word2" ...};


P.S your current checkPalindrome method will not work. Think about how you check String equality in Java (not with ==).

This post has been edited by Ryano121: 22 April 2012 - 04:42 AM

Was This Post Helpful? 0
  • +
  • -

#7 karabasf  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 202
  • View blog
  • Posts: 417
  • Joined: 29-August 10

Re: palindromes with string methods

Posted 22 April 2012 - 04:45 AM

Before I continue, string values are compared with the .equals() method, while "==" compares the memory locations. Thus, your isPalindrome method should be:

isPalindrome(String n)
   String x = Reverse(n)
   
   if(x.equals(n)
      return true;
   else
     return false;



As for your main method (and as stated in your other thread), you need to instantiate an array holding words. After this array is instantiated, you need to loop to determine whether an entry in that array is a palindrome or not.

E.g.

for(int i = 0; i < wordArray.length; i++){
  //Retrieve the element at i
  String temp = wordArray[i];
  
  //Check if the string temp is a palindrome with your isPalindrome method and take corresponding actions



Hope this helps you out ;)

Edit: rats, ninja'd by Ryano121 :shuriken:

This post has been edited by karabasf: 22 April 2012 - 04:46 AM

Was This Post Helpful? 0
  • +
  • -

#8 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3730
  • View blog
  • Posts: 6,017
  • Joined: 08-June 10

Re: palindromes with string methods

Posted 22 April 2012 - 04:48 AM

* Duplicate threads merged *
Please do not double post your questions!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1