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