The Artima Developer Community
Sponsored Link

Java Answers Forum
Converting my String Palindrome problem into a Integer

4 replies on 1 page. Most recent reply: May 11, 2003 6:06 AM by Charles Bell

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Neil

Posts: 5
Nickname: no158
Registered: May, 2003

Converting my String Palindrome problem into a Integer Posted: May 10, 2003 11:32 AM
Reply to this message Reply
Advertisement
Having a bit of troble trying to change the string commands in my program to integer ones
if you have any suggestions

Heres my code:

// User enters a positive number, no restrictions to how many chars
// program should test first if the integer is a palindrome
// if not a palindrome take the number that the user entered
// and add it to the same number but in reverse ex: 1442 is entered
// program will take 1442 + 2441 = 3883 then test new number to see if
// it is a palindrome

public class Palindrome

{

public static void main (String[] args)

{

String inStr;
StringBuffer revStr;

System.out.println("Palindrome Checker...");
System.out.print("Enter a string to check: ");

inStr = SavitchIn.readLine();
revStr = new StringBuffer(inStr);
revStr.reverse();


if (inStr.equalsIgnoreCase (revStr.toString()))

{

System.out.println(inStr + " is a palindrome. ");

}

else

{

System.out.println(inStr + " is not a palindrome. ");

}

}

}


Neil

Posts: 5
Nickname: no158
Registered: May, 2003

Re: Converting my String Palindrome problem into a Integer Posted: May 10, 2003 11:59 AM
Reply to this message Reply
This should be easy for me to convert but when I seach for IntegerBuffers I come up with squat. Or maybe it not integer buffers I need in this program any input would be most helpful

Thanks for any help on this matter
(Student hard at work)
-Neil-

Neil

Posts: 5
Nickname: no158
Registered: May, 2003

Re: Converting my String Palindrome problem into a Integer Posted: May 10, 2003 12:06 PM
Reply to this message Reply
Did I mention was new to the language, just finishing up my first semester in introduction to java. Going to end up taking a intermediate during the summer. Java is turning out to be more fun then I thought yet the beginning part is very time consuming, spent 5 hours on the palindrome problem as is, just a fyi as to who i am and where i'm coming from

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting my String Palindrome problem into a Integer Posted: May 10, 2003 5:31 PM
Reply to this message Reply
/** Palindromes are words that spell the same thing 
 *  when they are written forwards or backwards.
 *  This program test to see whether a string 
 *  entered by the user is a  Palindrome and 
 *  displays whether it is or not.
 */
public class Palindrome{
 
    /** Copnstructs a Palindrome object, which gathers
     *  a line of input from the user and tests if it
     *  is a Palindrome or not, and displays a message.
    */
    public Palindrome(){
        String test = getString();
        if (isPalindrome(test)){
            System.out.println(test + " is a Palindrone.");
        }else{
            System.out.println(test + " is not a Palindrone.");
        }
    }
    
    /** Runs the application. 
     */
    public static void main (String[] args){
        new Palindrome();
    }
    
    /** Prompts the user to eneter a string and 
     *  returns a line entered from standard input.
     */
    private String getString(){
        String s = "";
        try{
            System.out.println("Enter a String: ");
            java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            s = br.readLine();
        }catch(java.io.IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return s;
    }
    
    /** Tests if the input string is the same as its reverse. 
     */
    private boolean isPalindrome(String input){
        return input.equals(getReverse(input));
    }
    
    /** Reverses the characters in a string. 
     */
    private String getReverse(String s){
        String reverse = "";
        for (int i = s.length() - 1; i >= 0; i = i -1){
            reverse = reverse + s.charAt(i);
        }
        return reverse;
    }
} 

Charles Bell

Posts: 519
Nickname: charles
Registered: Feb, 2002

Re: Converting my String Palindrome problem into a Integer Posted: May 11, 2003 6:06 AM
Reply to this message Reply
/** Palindromes are words that spell the same thing 
 *  when they are written forwards or backwards.
 *  This program test to see whether a string 
 *  entered by the user is a  Palindrome and 
 *  displays whether it is or not.
 */
public class Palindrome{
 
    /** Copnstructs a Palindrome object, which gathers
     *  a line of input from the user and tests if it
     *  is a Palindrome or not, and displays a message.
     *  If not it adds the int value of the reverse of
     *  the number and check to see if it is a palindrome.
     */
    public Palindrome(){
        String first = getString();
        if (isPalindrome(first)){
            System.out.println(first + " is a Palindrone.");
        }else{
            System.out.println(first + " is not a Palindrone.");
            int p = toInt(first);
            String reverse = getReverse(first);
            int q = toInt(reverse);
            if ((p > 0) && (q > 0)){
                int r = p + q;
                String second = String.valueOf(r);
                if (isPalindrome(second)){
                    System.out.println(second + " is a Palindrone.");
                }else{
                    System.out.println(second + " is not a Palindrone.");
                }
            }
        }
    }
    
    /** Runs the application. 
     */
    public static void main (String[] args){
        new Palindrome();
    }
    
    /** Prompts the user to eneter a string and 
     *  returns a line entered from standard input.
     */
    private String getString(){
        String s = "";
        try{
            System.out.println("Enter a Positive Integer: ");
            java.io.InputStreamReader isr = new java.io.InputStreamReader(System.in);
            java.io.BufferedReader br = new java.io.BufferedReader(isr);
            s = br.readLine();
        }catch(java.io.IOException ioe){
            System.err.println("IOException: " + ioe.getMessage());
        }
        return s;
    }
    
    /** Tests if the input string is the same as its reverse. 
     */
    private boolean isPalindrome(String input){
        return input.equals(getReverse(input));
    }
    
    /** Reverses the characters in a string. 
     */
    private String getReverse(String s){
        String reverse = "";
        for (int i = s.length() - 1; i >= 0; i = i -1){
            reverse = reverse + s.charAt(i);
        }
        return reverse;
    }
    
    /** Converts a string into an int or -1 if not an int. 
     */
    private int toInt(String s){
        int n = -1;
        try{
            n = Integer.parseInt(s);
        }catch(NumberFormatException nfe){
            System.err.println("That was not a positive integer string.");
        }
        return n;
    }
} 

Flat View: This topic has 4 replies on 1 page
Topic: how download jdk Previous Topic   Next Topic Topic: HELP!how to apply J2EE to crystal report???

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use