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: Need help determining if number is a palindrome using an array!

  1. #1
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Need help determining if number is a palindrome using an array!

    I have:

    		public static void main(String[] args) {
    			while (true) {
    			display(check(retrieveInput()));
    			}
    			}
     
    			public static String retrieveInput() {
    			Scanner scan = new Scanner(System.in);
    			return scan.next();
    			}
     
    			public static boolean check(String input) {
    			boolean check = false;
    			try {
    			Integer.parseInt(input);
    			if (input.charAt(0)==input.charAt(4) && input.charAt(1)==input.charAt(3))
    			check = true;
     
    			} catch(Exception e) {
    			check = false;
    			}
     
    			return check;
    			}
     
    			public static void display(boolean check) {
    			if(check) System.out.println("Is a five-digit palindrome.");
    			else System.out.println("Is not a five-digit palindrome.");
    			}
    			}
    but the assignment is requiring me to use an array which I don't know how to do. Please Help!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help determining if number is a palindrome using an array!

    Strip off each power of 10 and store that value in the array
    Given 123 the array would be {1,2,3}


    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help determining if number is a palindrome using an array!

    Thanks for your reply. Sorry, this is my first java course. What do you mean by stripping off each power of ten?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help determining if number is a palindrome using an array!

    This has nothing to do with java. Its basic math
    The number 123 is 1 * 10^2 + 2 * 10^1 + 3 * 10^0

    The programming part would Use the % and / operators to do the work
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Feb 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Need help determining if number is a palindrome using an array!

    Thanks for the suggestions but I am not sure how to use those operators. Thanks again though.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Need help determining if number is a palindrome using an array!

    23 % 10 = 3
    23 / 10 = 2
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How do I return the current number of trades(number) from an array?
    By lovely92 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2012, 06:15 PM
  2. Labelling Number Array
    By andewor in forum Collections and Generics
    Replies: 0
    Last Post: October 14th, 2012, 03:51 AM
  3. Find Biggest Number in Array
    By jo15765 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: May 12th, 2012, 03:01 PM
  4. Number Array
    By TheJavaGuy in forum What's Wrong With My Code?
    Replies: 4
    Last Post: January 17th, 2011, 04:59 AM
  5. Generation of Palindrome number in Java
    By tina.goyal in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 26th, 2009, 08:49 AM