Wrox Programmer Forums
|
Java Basics General beginning Java language questions that don't fit in one of the more specific forums. Please specify what version.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Java Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old September 25th, 2006, 03:26 AM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default java problem

A numerical palindrome is a number whose digits read
the same forward and backward.
without using array method or string method

A sample input and output session is given below:
Enter integer number: 15951
Output: true
Enter integer number: 1551
Output: true
Enter input integer: 15931
Output: false
Enter input integer: –1

<Program terminates>

 
Old September 25th, 2006, 04:19 AM
Authorized User
 
Join Date: Mar 2006
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default

How to do in java

 
Old September 26th, 2006, 12:07 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

My implementation uses a linked list:

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

public class NumericPalindrome {

        public NumericPalindrome() {
                while(input()); // loop, just calling input method
                System.exit(0);
        }

        // one lot of input - take a line, read it char by char until encountering a
        // newline
        // return true if successfully read line and called isPalendromic,
        // exit(1) otherwise
        private boolean input() {
                // java console input is gnarly!
                InputStreamReader isr = new InputStreamReader(System.in);
                BufferedReader input = new BufferedReader(isr);
                char nextTok=' '; // character to read from inputstream into
                Integer myInt; // Integer object - will throw exception for
                                  // and make us exit(1) non numeric chars
                LinkedList list = new LinkedList(); // Well, it isn't an array!

                // rely on numberformat exception to meet the -1 exit clause
                try {
                        // -1 is the end of a line
                        while ((nextTok = (char)input.read())!=-1) {
                                // skip newline chars
                                if(nextTok == '\n' || nextTok == '\r') break;
                                myInt =
                                  new Integer( Integer.parseInt(nextTok+""));
                                // don't allow negative numbers
                                if (myInt.intValue()<0) break;
                                // append Integer to list
                                list.add(myInt);
                        }
                        System.out.println(isPalindromic(list));
                        return true;
                }
                catch (Exception ex) {
                        System.exit(1);
                }
                return false; // should never get here!
        }

        // read a linked list from outside inwards, i.e. compare elements starting with
        // first and last, then second and penultimate and so on.
        private boolean isPalindromic(LinkedList list) {
                        while (list.size() >= 2) {
                                if (! list.removeFirst().equals(list.removeLast()))
                                        return false;
                        }
                        if (list.size()<2)
                                return true; // i.e. there are no more elements, or only
                                             // one.
                        return false;
        }

        public static void main (String argv[]) {
                new NumericPalindrome();
        }

}


--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com
 
Old September 26th, 2006, 12:10 PM
Friend of Wrox
 
Join Date: Dec 2003
Posts: 488
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Oh yes, test data:

charlie@maui $ java NumericPalindrome
1
true
121
true
1234567890987654321
true
55634785647865487651213466846346328746254766753843 43242344324342432426546577
false
67
false
-1
charlie@maui $

--
Don't Stand on your head - you'll get footprints in your hair
                                           http://charlieharvey.org.uk
                                              http://charlieharvey.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
database problem[java] sunanda_chowdary Java Databases 0 February 21st, 2006 04:00 AM
Read First!! Java Security Problem Stephen Lam Servlets 1 June 20th, 2005 04:14 AM
Java Security Problem Stephen Lam Wrox Book Feedback 1 April 12th, 2005 01:56 PM
JAVA Security problem :) Stephen Lam All Other Wrox Books 1 March 14th, 2005 11:41 PM
Java code problem Loevet J2EE 5 October 23rd, 2003 08:42 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.