Member Avatar for sonicx2218

Hey guys..I'm a newbie, so please try to use easy words, as the more complicated stuff may make me bleed out of my ears ;)

So I'm trying to make this program where you type in a number using the xxx xxx xxxx style
The java program will then compare that number to its reverse, and if they are the same, it will print a message telling you it's a palindrome, otherwise it'll tell you it's not.

import java.io.*;
import java.util.StringTokenizer;

public class Super2
{
	public static void main(String[] args) throws IOException
	{
		String number;
		StringTokenizer st;
		StringBuffer first, middle, last; // could be string
		StringBuffer phonenumber = new StringBuffer();

		BufferedReader br = new
		BufferedReader(new InputStreamReader(System.in));
		System.out.print("Enter phone number using the xxx xxx xxxx format: ");
		number = br.readLine();
		st = new StringTokenizer(number, " ");
		first = new StringBuffer(st.nextToken());
		middle = new StringBuffer(st.nextToken());
		last = new StringBuffer(st.nextToken());
		phonenumber.append(first).append(middle).append(last);
		System.out.println("\nbacknumber = " + phonenumber.toString());
		System.out.println("\nnumber = " + phonenumber.reverse().toString());
		//if statement for comparing reverse
		if (phonenumber.reverse() == phonenumber)
		System.out.println("YESS");
		else
		System.out.println("NOOO");



		//convert string buffer to String


	}
}

The issue is...I don't know how to compare the 2 in an if statement..I can print out the reverse, and the normal, but I can't get my if statement to work, it'll always print the true statement when I run it..

Recommended Answers

All 3 Replies

break it up into chars and then compare it

use compareTo method of string to compare two string if it return 0 then they are equal else not equal.

Example:-

firstString.compareTo(secondString)==0 then equal.

use compareTo method of string to compare two string if it return 0 then they are equal else not equal.

Example:-

firstString.compareTo(secondString)==0 then equal.

why do you always make things harder then need be?
if you want to check two String objects for equality, why not use the equals method?
but, you may have noticed, this is not what the OP is trying to do.
he is trying to compare a String object to the reverse of another String object.

I would go with ilovejava's answer here.
step 1. check if they have the same length: if not, it's impossible for them to be palindromes return false
--. all next steps only run if the first step didn't return false
step 2. convert (at least) one of your Strings to a char array, or use the charAt() method to get a similar behaviour of your code
compare the Strings char by char, one String forwards, the other one backwards. you find a combination that isn't equal: return false
--. if none of the above steps returned false:
step 3. by default: return true

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.