So i am a college student and my assignment is to create a code that determines if an entered number is a palindrome or not.

the assignment is as follows:

"Write the following two methods:

//Return the reversal of an integer, i.e. revers(456) returns 654
public static int reverse(int number)

//Return true if number is palindrome
public static boolean isPalindrome(int number)

Use the reverse method to implement isPalindrome. a number is a palindrome if its reversal is the same as itself. write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome."

here is my attempt at the code. i know its missing a lot of things but i am truly and completely stuck. thanks for all the help in advance guys.

import java.util.Scanner;
public class Palindrome {
	public static void main(String[] args) {
	
		// Display name
		System.out.println("Programmed by ");
		
		// Get number from human
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter a number: ");
		int digit = input.nextInt();
		
		// Define variables
		int d = digit;
		int rev = 0;
		int i = reverse(digit);
		int j = isPalindrome(digit);
		
		// Create reverse method
		public static int reverse(int digit){
			int r;
			for (int x = 0; x <= digit; x++)
				r = digit%10;
				digit = digit/10;
				rev = rev * 10 + r;
		}
		
		// Create isPalindrome method
		public static boolean isPalindrome(int digit){
		}
	}
}

Edited 6 Years Ago by charat: n/a

a few things - isPalindrome returns a boolean, so you can't assign it to an int.
You would use it like

if (isPalindrome(i)) 
System.out.println("i? si!");
else
System.out.println("o, no.");

Also your loop index is not exactly what I'd use. I suspect it works, at least some of the time, but you don't want to use a for loop this way. Try a while loop - while there's more of digit to process, keep processing.

And you need a return for your reverse() method.

Edited 6 Years Ago by jon.kiparsky: n/a

im not sure what you mean by the isPalindrome returns a boolean. also, how would i go about using a while loop? could you give me an example?

from your code - probably your teacher gave you this. You should understand it.

public static boolean isPalindrome(int digit)

public means it can be called by any class that can reach it.
static means it is called from the class, not from the instance. What this means gets sort of deep, but one thing it means is that you can call it from your main() method, which is also static.

boolean is the return type. The last thing this method does will be to return a value of boolean type. This can be a simple true or false, ie

return true;   //returns true

or it can be a variable of type boolean, ie

boolean verdad = true;
return verdad;  //returns true

or it can be an expresion that evaluates to a boolean value:

return verdad = false;  // returns false

In any case, when the code comes to a return statement, it stops working in that method, and it goes back to where the method was called, with that value in hand.

If you call

public boolean myMethod()  { return true;}

like so:

boolean foo = myMethod();

or like so:

if (myMethod()) { doStuff();}

you can think of the return value as substituting for the method call - foo will in this case get the value of "true" and the doStuff() method will be called.

So you can see why you can't assign this value to an int variable - it's like telling the compiler "int i = true;". Doesn't make sense.

While loops are pretty simple. Take a natural language:
"while I'm eating breakfast, I don't want to be disturbed".

As long as the condition "I'm eating breakfast" holds, you are not to interrupt.

Okay, in java, same thing:

while (jon.isEatingBreakfast())
{
buzzOff();
}

This will continue to loop through "buzzOff()" as long as a call to my isEatingBreakfast() method returns true.

You can use any expression in the parens, as long as it evaluates to a boolean. While (1>2) will never do anything, while (1<2) will loop forever, and while (i-- > 0) will repeat until i decrements to 0 and the loop condition becomes false.

if possible, let me repost my edited code.

import java.util.Scanner;
public class Palindrome {
	public static void main(String[] args) {
	
		// Display name
		System.out.println("Programmed by ");
		
		// Get number from human
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter a number: ");
		int digit = input.nextInt();
		
		// Define variables
		int d = digit;
		int i = reverse(d);
		int j = isPalindrome(d);
		
		// Create reverse method
		public static int reverse(int number){
			number = 
		}
		
		// Create isPalindrome method
		public static boolean isPalindrome(int number){
			if ( == digit)
				System.out.println(digit+" is a Palindrome.")
			else ( != digit)
				System.out.println(digit+" is NOT a Palindrome.")
		}
	}
}

firsly, i suspect that my variables are wrong in the first part but i can't tell how i could fix them.

// Get number from human
		Scanner input = new Scanner(System.in);
		System.out.print("Please enter a number: ");
		int digit = input.nextInt();
		
		// Define variables
		int d = digit;
		int i = reverse(d);
		int j = isPalindrome(d);

secondly, i have no idea how i would use a while loop within the reverse method and using the same variables as stated before

// Create reverse method
		public static int reverse(int number){
			number = 
		}

Lastly, i think i have the last isPalindrome method fine, it just seems to be missing a variable that i have yet to determine.

any help is much appreciated. thank you.

@jon
im still not sure how i would be able to incorporate each of those you mentioned. i have no idea where i could place the while loop. are you also saying that i could just us a boolean instead of a methos? the assignment calls for me to use a method in that sense though. and the program provided was one i wrote but doesnt work.

Okay, so your original for loop repeated until x<=digit

That's okay for something like digit =11:

going into the first loop, x= 0, digit = 11, x<=11, so we perform the loop

second check, x=1, digit = 1, x<=digit, so do the loop

third check, x = 2, digit = 0, x>digit so we stop. rev is now 11, so the method works, right?

Well, what if we try 111?
first check, x = 0, digit = 111, do the loop. rev now = 1
second check, x = 1, digit = 11, do the loop, rev now = 11
third check, x = 2, digit = 1, stop looping, rev still = 11.

Not what you wanted, is it?

Okay, so what if we make it a while loop.

while (digit >0)
{ 
 //same code as your original for loop
}

first check: digit= 111, which is greater than 0, so set rev = 1
second check: digit = 11, still >0, so set rev = 11
third check: digit = 1, still > 0, so set rev = 111
fourth check: digit = 0, not >0, so stop. rev == 111, which is what you wanted.

That's how you'd use a while loop there. Please read up on this control structure, it's even more basic than the for loop (a for loop can always be rewritten as a while loop). All it does is repeat a block of code as long as a given condition evaluates to true.

And while you're reviewing, please take a look at what "boolean" means. I'm not here to write a textbook, you should have one handy.

i figured it out. thanks... you didn't really help though...

Terribly sorry to be so useless. Would you care to post what you've figured out so the next person having this problem can benefit from your experience?

I have a project similar to this and I'm having some trouble with it. Would you mind posting your successful code so I can use it as a reference for my project?

This question has already been answered. Start a new discussion instead.