# Palindrome

Page 1 of 1

## 8 Replies - 5723 Views - Last Post: 27 September 2010 - 07:25 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=192325&amp;s=fe135074220cad1307b9b76731f7eb39&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 kwjelly20

Reputation: 0
• Posts: 30
• Joined: 27-September 10

# Palindrome

Posted 27 September 2010 - 02:50 PM

Assignment: Use the reverse method to implement isPalindrome(). A number is a palindrome if its reversal is the same as itself. Your program will include a main() method to exercise these methods by prompting the user to enter an integer and reports success or failure of the palindrome comparison. The user should be prompted five times to enter a number; use a for-loop to implement this.

I have written the script but reached a stopping part on the isPalindrome portion at the end. I have tried running the and keep recieveing a message such a [}parsing}}.

Can someone guide me in the right direction.

``` import java.util.Scanner;

public class Palindrome{
/**Main Method */
public static void main(String args[]){
//Create a Scanner
Scanner input = new Scanner(System.in);

//Prompt user to enter three intergers
System.out.print("Enter first integer: ");
int n1 = input.nextInt();
System.out.print("Enter second integer: ");
int n2 = input.nextInt();
System.out.print("Enter third integer: ");
int n3 = input.nextInt();
System.out.print("Enter fourth integer: ");
int n4 = input.nextInt();
System.out.print("Enter fifth integer: ");
int n5 = input.nextInt();

System.out.printf( "The total number is " + n1 + n2 + n3 + n4 + n5);

}
}
/** Return the reversal of an integer, i.e. reverse(456) returns 654 */
public class method1{

public static int reverse( int n1, int n2, int n3, int n4, int n5){
return reverse;
}

/** Return true if number is palindrome */
public class PalindromeCheck {

public static boolean isPalindrome(int number)
```

Is This A Good Question/Topic? 0

## Replies To: Palindrome

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• Joined: 27-December 08

## Re: Palindrome

Posted 27 September 2010 - 02:57 PM

Why are you declaring a new class for each method you want to define? This is poor practice. Also, check your braces. You do not end the method1 class, end the PalindromeCheck class, or places braces around the body of the isPalindrome() method. In your reverse() method, I do not see where you define the reverse variable, so you should be getting a Cannot Find Symbol error there. Not that it's good practice to name a variable and method in the same class the same thing.

### #3 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Palindrome

Posted 27 September 2010 - 03:00 PM

There are many ways to implement that:
- Your isPalindrome() can receive 2 parameters: the original number and its reverse version
so it will be true if both numbers are equals

```static boolean isPalyndrome(int num, int itsReverse) {
return num == itsReverse;
}

```

or your isPalindrome() method can receives only the number as parameter, reverse it and then compare

```static boolean isPalyndrome(int num) {
return num == reverse(num);
}

```

### #4 kwjelly20

Reputation: 0
• Posts: 30
• Joined: 27-September 10

## Re: Palindrome

Posted 27 September 2010 - 03:14 PM

I guess my question really relies within line 29 thru 32, how do I justify a true or false method because in line 32 this starts my print output for the final answer of the program. Line 32 should already be considered as reversed just recieving the input and producding true or false statement.

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12318
• Posts: 45,417
• Joined: 27-December 08

## Re: Palindrome

Posted 27 September 2010 - 03:18 PM

I think pbl showed you an excellent example. Now to work on your reverse() method. Hint- it should only accept one param, and return its reverse. You may consider looking at the StringBuilder class which has a reverse() method.

### #6 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Palindrome

Posted 27 September 2010 - 04:12 PM

macosxnerd101, on 27 September 2010 - 04:18 PM, said:

You may consider looking at the StringBuilder class which has a reverse() method.

I would do it for a String surely not for an int:

```int reverse = 0;
while(num > 0) {
reverse *= 10;
reverse += num % 10;
num /= 10;
}

```

As far the original code is concerned there 3 class declarations in that code
to achieve your goal you will need only one
You need to do some cleanup... and why adding your numbers ? You cut & pasted another homework ?

This post has been edited by pbl: 27 September 2010 - 05:39 PM
Reason for edit:: =/ 10 not 0

### #7 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Palindrome

Posted 27 September 2010 - 05:11 PM

pbl, on 27 September 2010 - 06:12 PM, said:

macosxnerd101, on 27 September 2010 - 04:18 PM, said:

You may consider looking at the StringBuilder class which has a reverse() method.

I would do it for a String surely not for an int:

```int reverse = 0;
while(num > 0) {
reverse *= 10;
reverse += num % 10;
num /= 0;
}

```

Won't that code throw a DivideByZeroException?

### #8 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Palindrome

Posted 27 September 2010 - 05:41 PM

Dogstopper, on 27 September 2010 - 06:11 PM, said:

pbl, on 27 September 2010 - 06:12 PM, said:

macosxnerd101, on 27 September 2010 - 04:18 PM, said:

You may consider looking at the StringBuilder class which has a reverse() method.

I would do it for a String surely not for an int:

```int reverse = 0;
while(num > 0) {
reverse *= 10;
reverse += num % 10;
num /= 0;
}

```

Won't that code throw a DivideByZeroException?

Fixed was just checking if you were checking

### #9 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Palindrome

Posted 27 September 2010 - 07:25 PM

I passed! YAY!