Click to See Complete Forum and Search --> : Palindrome


merxais
03-02-2005, 03:47 AM
Write a complete program to test whether the number entered by the user is a palindrome or not. Palindrome is a number whose value does not change even it is reversed. For examples: 232 and 4554 is a palindrome. The program should execute repeatedly until the user choose to quit.

No idea how to do... anyone help pls?

BuezaWebDev
03-02-2005, 05:43 AM
Originally posted by merxais
Write a complete program to test whether the number entered by the user is a palindrome or not. Palindrome is a number whose value does not change even it is reversed. For examples: 232 and 4554 is a palindrome. The program should execute repeatedly until the user choose to quit.

No idea how to do... anyone help pls?


import java.util.Scanner;

/**
* Program that checks if an input value is
* a palindrome (1001, 1111, etc, are all palindromes).
*
* @author Jaime Bueza
*/
public class Palindrome {
public static void main(final String[] args) {
final boolean isPalindrome = false;
final Scanner in = new Scanner(System.in);
int input = 1; //Set default value.

System.out.println("Usage: Enter 0 To Quit Program");

do {
System.out.print("Enter a value: ");
input = in.nextInt();
/*
if statements go here to check if the
input value is a palindrome. I believe there
is an equation to find out if an integer is
a palindrome. I'm not sure though. You might
want to try and google it.

You will also want to add an if statement
like...

if(isPalindrome == true) {
System.out.println(input + " is a palindrome!");
} else {
System.out.println(input + " is NOT a palindrome.");
}
*/
} while(input != 0); //0 is Sentinel value.
}
}


Hope this helps in terms of a skeleton. Sorry if the comments suck, mate, lol, I did it in 5 minutes. :( If you don't understand a part of the code, please say so. I'll be happy to explain to you. :)

Basically, with that do-while loop, it won't terminate until the user inputs 0, which is the sentinel value for the termination of the loop.

It will keep looping--asking the user to input a value and it will also say if the input is a palindrome, assuming you code the correct equation to determine the palindrome.

Good luck!

Kind regards,
Jaime Bueza

merxais
03-02-2005, 10:12 AM
do i need any file to support to run this? because i got error ... it seems dunno what is Scanner... :confused:

Pittimann
03-02-2005, 10:18 AM
Hi!Originally posted by merxais
do i need any file to support to run this? because i got error ... it seems dunno what is Scanner... :confused: What you need is to have a look at the crossposted thread in the js forum. You are dealing with JAVA here and BuezaWebDev has wasted his/her time.

Pit

merxais
03-02-2005, 10:44 AM
I did search for the palindrom in this JAVA forum but none of it and in Javascript is not the same...

i think u need to know if got or not other this palindrome be4 u scold someone Pit.

Pittimann
03-02-2005, 11:13 AM
I apologize for having thought that this was a mistake you made. But - please be honest: what idea do you have of java??

merxais
03-02-2005, 12:25 PM
just beginner... lecturer asking something she havent teaches us...

i just know import JOptionPaneand the maths...
ummm..
i was thinking to get the answer with the formula
x=x%10;
x=x/10;

umm... dunnow how to repeat the calculation... then.. trying and trying...

i think to use InputDialog for input?

Khalid Ali
03-02-2005, 01:38 PM
Since its your home work, I don't feel particularly good to providing you the complete solution.However here is the logical flow that I can think of a way to get that done.
1. get user input.
2. make sure that length of the input is at least 2 chars
3. divide the string into 2 parts
4. e.g firstHalf and secondHalf.
5. reverse the second half string
6. now compare both of them they must be equal

I hope this helps.

BuezaWebDev
03-02-2005, 03:39 PM
An interesting way to go about doing this would be to use the Comparable interface.


public class Palindrome implements Comparable {
//etc.
}


With the Comparable interface, you can use:

Object.compareTo()

--

Oh yeah, and I think I totally misunderstood the original question. I thought the question was asking for integer inputs only. :| That makes it a lot easier. lol.


http://www.devx.com/tips/Tip/14921

Here's a source to figure out if a string is a palindrome written in VB.

merxais
03-02-2005, 10:56 PM
yeah.. integer.. but i dunno how..
Khalid Ali.. didnt learn the length thing.. how to do.. sad..

can that input is integer just use :
import javax.swing.JOptionPane;

:confused:

ray326
03-02-2005, 11:29 PM
Make a String representation of the value of the int. Make a second String that is the reverse of the first. Compare the two Strings. If they are equal then you've got a palindrome.

merxais
03-02-2005, 11:51 PM
import javax.swing.JOptionPane;

class palindrome
{
public static void main(String args[])
{
int w,x,y;
String s=JOptionPane.showInputDialog("Enter a number: ");
x=Integer.parseInt(s);
String s1=" ";

while (x>0)
{
y=x%10;
x=x/10;


JOptionPane.showMessageDialog(null,"Ans: "+y);
System.exit(0);
}
}
}


how to make to get the all reverse value?? sad... only can get one!!
cant get the reverse value.. i can compare.. help pls..

ray326
03-03-2005, 09:57 PM
Here's some hints copied off my BeanShell window.

bsh % String s=JOptionPane.showInputDialog("Enter a number: ");
bsh % sb2=new StringBuffer();
bsh % for (int i=s.length()-1; i>=0; i--) sb2.append(s.charAt(i));

Man, that interactive Java is fun. 8)