Palindrome

Java assigment

Page 1 of 1

8 Replies - 3834 Views - Last Post: 27 September 2010 - 07:25 PM Rate Topic: -----

#1 kwjelly20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,373
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • 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);
}


Was This Post Helpful? 1
  • +
  • -

#4 kwjelly20  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • 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.
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,373
  • 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. :)
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Palindrome

Posted 27 September 2010 - 04:12 PM

View Postmacosxnerd101, 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

Was This Post Helpful? 1
  • +
  • -

#7 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Palindrome

Posted 27 September 2010 - 05:11 PM

View Postpbl, on 27 September 2010 - 06:12 PM, said:

View Postmacosxnerd101, 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?
Was This Post Helpful? 0
  • +
  • -

#8 pbl  Icon User is offline

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

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Palindrome

Posted 27 September 2010 - 05:41 PM

View PostDogstopper, on 27 September 2010 - 06:11 PM, said:

View Postpbl, on 27 September 2010 - 06:12 PM, said:

View Postmacosxnerd101, 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 :turned: was just checking if you were checking :)
Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,021
  • Joined: 15-July 08

Re: Palindrome

Posted 27 September 2010 - 07:25 PM

I passed! YAY!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1