954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Comparing Strings using if statement

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..

sonicx2218
Light Poster
31 posts since Feb 2012
Reputation Points: 10
Solved Threads: 0
 

break it up into chars and then compare it

ilovejava
Junior Poster in Training
77 posts since Sep 2011
Reputation Points: 10
Solved Threads: 2
 

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.

rushikesh jadha
Junior Poster in Training
89 posts since Dec 2011
Reputation Points: 4
Solved Threads: 11
 

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

stultuske
Posting Sensei
3,137 posts since Jan 2007
Reputation Points: 1,114
Solved Threads: 433
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You