Solved

5 digit Palindrome

Posted on 2006-12-19
1,814 Views
Last Modified: 2012-06-27
Does anyone have any feedback on how to write a program that inputs a 5-digit integer from the keyboard and prints if the input number is a palindrome or not.

This is the program I wrote:

import java.util.*;
 class Tester22
{
      public static void main(String args[])
      {
      int palindrome number =0;
            int n1 = Integer.parseInt(number);
            Scanner input = new Scanner(System.in);
            System.out.println("Please input a 5-digit number);
            int given = input.nextInt();
            If(number >="Is a palindrome number");
            else
            If (number >="Is not a palindrome number");
            System.out.println(" number + palindrome");
            System.out.println("number += not a palindrome");
      }
}
The compilation left me with this result:'

';' expected')'                      line 6


  ')' expected                      line 10


'else' without 'if'                 line  12


I have tried everything to make this program compile.   I will keep at it!  While someone gives me a hand.



Fred








0
Question by:Fred_45559
    17 Comments
     
    LVL 16

    Expert Comment

    by:suprapto45
    Hi Fred,

    You have MANY mistakes.

    >>int palindrome number =0;
    There is no such thing to have a variable with space in it. Try to change it like palindrome_number.

    >>System.out.println("Please input a 5-digit number);
    Where is the ending double quote?

    >>"If(number >="Is a palindrome number");"
    If in Java do NOT have capital codes and it should not be ended up with semicolon (;).

    Please fix it and get back to us.

    David
    0
     

    Author Comment

    by:Fred_45559
    Does anyone have any feedback on how to write a program that inputs a 5-digit integer from the keyboard and prints if the input number is a palindrome or not.


    I made some corrections to this program and I still cannot get the program to compile.

     import java.util.*;
     class Tester22
    {
         public static void main(String args[])
         {
         int palindrome_number=0;
              int n1 = (Integer.parseInt(number)
              Scanner kbi = new Scanner (System.in);
              System.out.println("Please input a 5-digit number");
              int given = input.nextInt();
              If(number >="is a palindrome number");
              If (number >="is not a palindrome number");
              System.out.println(" number + palindrome");
              System.out.println("number += not a palindrome");
         }

    0
     
    LVL 35

    Accepted Solution

    by:
    The easiest would be to turn the number into a string and check it with a reverse string.

    public static void main(String args[]) throws IOException
         {
              int palindrome_number=0;
              System.out.println("Please input a 5-digit number");
              BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
              String line = input.readLine();
              boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString());
              if (palindrome)
              {
                  System.out.println("it is a palindrome");
              }
              else
              {
                  System.out.println("it is not a palindrome");
              }
         }

    BTW this is the best place to start: http://java.sun.com/docs/books/tutorial/



    0
     
    LVL 6

    Expert Comment

    by:SamsonChung
    The following is a logical error...

    If(number >="Is a palindrome number");
    else
    If (number >="Is not a palindrome number");

    aside from the fact that you have not follow java syntax, it does not make logical sense.

    in Pseudo code, it would say,

    if number >= "Is a palindrome number" then
      //do nothing
    else
      if number >= "is not a palindrome number" then
        //do nothing
      end if
    end if

    you see the logical error? you are comparing a number to a String, and then doing nothing with it.

    0
     
    LVL 6

    Expert Comment

    by:SamsonChung
    I rewrote the code for you, using gironis' logic, with a small twist, this now works to display whether it is a normal palindrome or a palindrome number.


    import java.util.*;
    class Tester22{

      public Tester22(){
        //display the message to ask for an input
        System.out.println("Please input a 5-digit number");
        //Read the input
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String line = input.readLine();
        //compare ignoring cases, if line is equal to the reverse of line.
        boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString());
        String output = "";
        if (palindrome){
          output = line + " is a palindrome";
        } else {
          output = line + " is not a plaindrome";
        }
        try{
          int i = Integer.parseInt(line);
          output = output + " number";
        } catch (Exception e){

        }
        System.out.println(output);
      }
      public static void main(String args[]){
         int palindrome_number=0;
              int n1 = (Integer.parseInt(number)
              Scanner kbi = new Scanner (System.in);
              System.out.println("Please input a 5-digit number");
              int given = input.nextInt();
              If(number >="is a palindrome number");
              If (number >="is not a palindrome number");
              System.out.println(" number + palindrome");
              System.out.println("number += not a palindrome");
      }
    }
    0
     
    LVL 2

    Expert Comment

    by:saintsairforce
    The only problem I see with girionis code is that it is possible for the user to enter in letters and it will be accepted. Another way to process the number would be to have a simple logic if statement

    if((number/10000==number%10)&&(number/1000==(number%100-number%10)))
                   //then it's a palidrom. Assumming is 5 numbers long
    0
     
    LVL 2

    Expert Comment

    by:saintsairforce
    Small correction

    if((number/10000==number%10)&&(number/1000==(((number%100)-number%10)/10))
                   //then it's a palidrom. Assumming is 5 numbers long
    0
     

    Author Comment

    by:Fred_45559
    I utilized girionis' and saintsairforce's advice together and I still cannot get this program to compile?  I just researched http://java.sun.com/docs/books/tutorial/ .  Maybe I am just putting strings in the wrong places.

    import java.util.*;
    class Tester10
      {
        public static void main(String args[]) throws IOException
         {
              int palindrome_number=0;
              System.out.println("Please input a 5-digit number");
              BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
              String line = input.readLine();
              boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString)));
    if((number/10000==number%10)&&(number/1000==(((number%100)-number%10)/10))
                   //then it's a palidrom. Assumming is 5 numbers long))

              if (palindrome)
         }
                  System.out.println("it is a palindrome");
              }
              else
              }
              System.out.println("it is not a palindrome");
              }
         }


    Once I compile the program, it gives me

    ';' expected   on line 10

    ')' expected on line 12

    illegal start of expression on line 15

    <identifier> expected on line 16

    'class' or 'interface' expected on line 18



    0
     
    LVL 35

    Expert Comment

    by:girionis
    When you open and close an if statement you have to do it inside the closing brackets like

    if
    {
    }

    but you have

    if
    }
    }

    Correct these errors and try again, or just copy and paste my programme and it will compile. Then modify it accordingly.
    0
     

    Author Comment

    by:Fred_45559

    girionis, I tried your code from above and it still did not work.   Can you give me an idea on how to correct it.  I researched I/O Exceptions and BufferReader.  And I didn't get anything from it.

    class Tester34
    {

     public static void main(String args[]) throws IOException
         {
              int palindrome_number=0;
              System.out.println("Please input a 5-digit number");
              BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
              String line = input.readLine();
              boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString());
              if (palindrome)
              {
                  System.out.println("it is a palindrome");
              }
              else
              {
                  System.out.println("it is not a palindrome");
              }
         }
    }

    ___________________________________________________________________
    cannot find symbol class IOException    line 5

    cannot find symbol class BufferedReader    line 9

    cannot find symbol class BufferedReader    line 9

    cannot find symbol class InputStreamReader   line 9

    0
     

    Author Comment

    by:Fred_45559
    Tried this code also:

    import java.util.*;
    class Tester35
    {

      public Tester35(){
        //display the message to ask for an input
        System.out.println("Please input a 5-digit number");
        //Read the input
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        String line = input.readLine();
        //compare ignoring cases, if line is equal to the reverse of line.
        boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString());
        String output = "";
        if (palindrome){
          output = line + " is a palindrome";
        } else {
          output = line + " is not a plaindrome";
        }
        try{
          int i = Integer.parseInt(line);
          output = output + " number";
        } catch (Exception e){

        }
        System.out.println(output);
      }
      public static void main(String args[]){
         int palindrome_number=0;
                 int n1 =(Integer.parseInt(number)
              Scanner kbi = new Scanner(System.in);
              System.out.println("Please input a 5-digit number");
              int given = input.nextInt();
              If(number >="is a palindrome number");
              If (number >="is not a palindrome number");
              System.out.println(" number + palindrome");
              System.out.println("number += not a palindrome");
      }
    }
    ___________________________________________________________

    ')' expected                  on line 30        Line thirty already has a ')'      

    I getting more confused as I go along.  I just going to continue to hack it this.  What does that ')' means if it already have one.   Maybe my compiler has a hex on it!!!!!!  
    0
     
    LVL 35

    Expert Comment

    by:girionis
    >cannot find symbol class IOException    line 5
    >
    >cannot find symbol class BufferedReader    line 9
    >
    >cannot find symbol class BufferedReader    line 9
    >
    >cannot find symbol class InputStreamReader   line 9

    You have to import all these packages. Try

    import java.io.*'
    0
     

    Author Comment

    by:Fred_45559
    I tried     import java.io.*'


    It compiled:

    illegal line end in character literal        on line 1


    I will research it and get back with you!
    0
     

    Author Comment

    by:Fred_45559
    I researched and found import java.io.*;  and I tested it .

    It compiled:


    cannot find symbol class Scanner   on lline 8

    cannot find symbol class Scanner   on lline 8

    cannot find symbol variable number     on line  10



     
    0
     
    LVL 35

    Expert Comment

    by:girionis
    You have to import the Scanner class also.
    0
     

    Author Comment

    by:Fred_45559
    This program compiled, I appreciate your help!  I see that I was not using if  and else statements correctly.  I was trying to print from the scanner when I didn't have to.  The import java.io.*  statement I difinitely needed.  Everytime you would give me advice.  I would research it and apply it.  Thanks.  I need expert like you for that reason.  I was working on this program for 3 days.    Thanks

    import java.io.*;
    class Tester35
    {

     public static void main(String args[]) throws IOException
         {
              int palindrome_number=0;
              System.out.println("Please input a 5-digit number");
              BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
              String line = input.readLine();
              boolean palindrome = line.equalsIgnoreCase(new StringBuffer(line).reverse().toString());
              if (palindrome)
              {
                  System.out.println("it is a palindrome");
              }
              else
              {
                  System.out.println("it is not a palindrome");
              }
         }
    }
    0
     
    LVL 35

    Expert Comment

    by:girionis
    Thank you for accepting, glad I was of help. A few things to notice though

    a) the palindrome_number variable is never used, so you might as well delete it.
    b) you do not really need to use equalsIgnoreCase, you might as well use equals()

    boolean palindrome = line.equals(new StringBuffer(line).reverse().toString());
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Lean Six Sigma Project Manager Certification

    There are many schools of thought around successful project management, but few as highly regarded as the Six Sigma and Lean methods. With 37 hours of learning, this training will explain concrete processes for increasing efficiency and limiting wasted time and effort.

    Suggested Solutions

    For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
    By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

    659 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    31 Experts available now in Live!

    Get 1:1 Help Now