Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Code not printing correctly

  1. #1
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Exclamation [Solved] Code not printing correctly

    I'm creating a program for school that tells whether or not a phone number is a palindrome. A palindrome is when a number is reversed and the order is the same. The program is below, but the problem is that the program that does not print right. In the last method it's supposed to return a true or false to the main function, but no matter what number is entered it always goes to the false path and if the true and false were to be reversed then it would print everything as being true even when it shouldn't be. Let me know if I need to clarify anything. Thank You.

    Here's the code:



    import java.io.*;
    import java.util.StringTokenizer;
    public class palindrome
    {
    public static void main(String[] args) throws IOException
    {
    String phone;
    BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
    System.out.print("Enter a telephone number (xxx) xxx-xxxx: ");
    phone = br.readLine();
    phone = remove(phone);
    System.out.println("Original number: " + phone);
    if (reverse(phone))
    System.out.println("This is a palindrome");
    else
    System.out.println("This is not a palindrome");
    }

    public static String remove(String telephone)
    {
    StringTokenizer num;
    StringBuffer numbers, numbers2, numbers3;
    StringBuffer allnumbers = new StringBuffer();
    num = new StringTokenizer(telephone, " ()-");
    numbers = new StringBuffer(num.nextToken());
    numbers2 = new StringBuffer(num.nextToken());
    numbers3 = new StringBuffer(num.nextToken());
    allnumbers.append(numbers).append(numbers2).append (numbers3);
    telephone = allnumbers.toString();
    return telephone;
    }

    public static boolean reverse(String palindrome)
    {
    StringBuffer numbers = new StringBuffer();
    numbers = new StringBuffer(palindrome).reverse();
    palindrome = numbers.toString();
    System.out.println("palindrome: " + palindrome);
    System.out.println("numbers: " + numbers);
    if (palindrome.equals(numbers))
    return true;
    else
    return false;
    }
    }
    Last edited by Movies32; March 8th, 2010 at 04:22 PM.


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Code not printing correctly

    Is the output from the following two lines what you expect it to be?
    System.out.println("palindrome: " + palindrome);
    System.out.println("numbers: " + numbers);

    I'm guessing it's not. This sets palindrome equal to numbers (roughly speaking):
    palindrome = numbers.toString();

    I think if you just remove that line, your program will work as intended.

  3. #3
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Code not printing correctly

    Hi I have debugged your code and here is the solution to it.

    You have to change your code on two places:

    1. Remove this particular line: palindrome = numbers.toString();

    Why I said this is Firstly you are reversing your original string by reverse function and storing it into numbers variable.
    And then again you are storing the same reversed number into palindrome.
    So now both palindrome and numbers become same. And it will always return true for an unpalindrome number also.

    2. Why it is going into the false case.
    Because you are matching a string with a string buffer object. It will never be equal.
    So you change this particular line " if (palindrome.equals(numbers)) " to " if (palindrome.equals(numbers.toString())) "

    You will now be able to run.

    Give your feedback.

  4. The Following 2 Users Say Thank You to jassi For This Useful Post:

    Movies32 (March 8th, 2010), Shambolic (February 6th, 2010)

  5. #4
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: Code not printing correctly

    Ah, good point jassi. A String can be compared to a StringBuffer using the String.contentEquals(StringBuffer) method; String.equals(Object) will always return false if the Object is not a String. See here: String (Java 2 Platform SE 5.0)

    Movies32, you should always make sure you understand the specification of methods before you use them!

  6. The Following User Says Thank You to Shambolic For This Useful Post:

    jassi (February 6th, 2010)

  7. #5
    Member
    Join Date
    Feb 2010
    Location
    Dehradun, India
    Posts
    37
    Thanks
    1
    Thanked 7 Times in 6 Posts

    Default Re: Code not printing correctly

    Yeah you are absolutely right Shambolic..Good point.

    Thanks...

    Hi Movies32, Are you able to run it now?

  8. #6
    Junior Member
    Join Date
    Feb 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Smile Re: Code not printing correctly

    Yes the program works now. Thank you both so very much. I knew it was something minor that was messing it up. Thank you.

Similar Threads

  1. JOptionPane Question/ Printing
    By 03EVOAWD in forum AWT / Java Swing
    Replies: 2
    Last Post: August 31st, 2009, 10:17 AM
  2. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 09:15 AM
  3. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 07:57 AM
  4. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 02:50 PM
  5. Printing a Histogram Help - Arrays
    By Mock26 in forum Collections and Generics
    Replies: 1
    Last Post: June 4th, 2009, 05:49 AM

Tags for this Thread