Link to home
Start Free TrialLog in
Avatar of Fred_45559
Fred_45559

asked on

5 digit Palindrome

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








Avatar of suprapto45
suprapto45
Flag of Singapore image

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
Avatar of Fred_45559
Fred_45559

ASKER

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");
     }

ASKER CERTIFIED SOLUTION
Avatar of girionis
girionis
Flag of Greece image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.

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");
  }
}
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
Small correction

if((number/10000==number%10)&&(number/1000==(((number%100)-number%10)/10))
               //then it's a palidrom. Assumming is 5 numbers long
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



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.

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

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!!!!!!  
>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.*'
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!
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



 
You have to import the Scanner class also.
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");
          }
     }
}
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());