473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Ned help in PALINDROME

26
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it.

If I enter 33633, 11211, 45554, it will return

It's a Palindrome!

and if I enter 33636 or 11214, it returns

It's not a Palindrome!

now my problem is if i enter 00000, it got some errors the one below:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.javaL687)
at palindrome4.main(palindrome4.java:19)

And if I only enter 1,2,3 or 4 digits like 33 or 112, i got the same error.

String index out of range: 1 or 2 or 3 or 4 (depending on the input)

Here's my code below..


import java.util.*;

public class palindrome4
{
public static void main (String args[])
{

Scanner input = new Scanner (System.in );
String num
int number, num1, num2, num3, num4, num5;

System.out.print( "Enter a 5-digit integer: " );
number = input.nextInt();

len = number + "";
num1 = num.charAt(0);
num2 = num.charAt(1);
num3 = num.charAt(2);
num4 = num.charAt(3);
num5 = num.charAt(4);

if (num1 == num5 && num2 == num4 )
System.out.println ( "wOoOt! It's a PALINDROME!" );
else
System.out.println (" Sorry! It's not a PALINDROME.");

Calendar date = Calendar.getInstance();
System.out.printf( "\nProcessed on %tc by \nElvar de los Santos\n", date );

}
}
Jul 10 '07 #1
2 2772
You should check if the length of the input string is equal to 5.
if yes only then you should move foward.
you are extracting charAt upto 5 chars so it will obviously give the problem of out of bound.
Jul 10 '07 #2
r035198x
13,262 8TB
aloha people! I need help in my java palindrome program. It's a 5-digit palindrome checker. The code below is running good but i got a few problems on it.

If I enter 33633, 11211, 45554, it will return

It's a Palindrome!

and if I enter 33636 or 11214, it returns

It's not a Palindrome!

now my problem is if i enter 00000, it got some errors the one below:

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 1
at java.lang.String.charAt(String.javaL687)
at palindrome4.main(palindrome4.java:19)

And if I only enter 1,2,3 or 4 digits like 33 or 112, i got the same error.

String index out of range: 1 or 2 or 3 or 4 (depending on the input)

Here's my code below..


import java.util.*;

public class palindrome4
{
public static void main (String args[])
{

Scanner input = new Scanner (System.in );
String num
int number, num1, num2, num3, num4, num5;

System.out.print( "Enter a 5-digit integer: " );
number = input.nextInt();

len = number + "";
num1 = num.charAt(0);
num2 = num.charAt(1);
num3 = num.charAt(2);
num4 = num.charAt(3);
num5 = num.charAt(4);

if (num1 == num5 && num2 == num4 )
System.out.println ( "wOoOt! It's a PALINDROME!" );
else
System.out.println (" Sorry! It's not a PALINDROME.");

Calendar date = Calendar.getInstance();
System.out.printf( "\nProcessed on %tc by \nElvar de los Santos\n", date );

}
}
Remember
1.) The program only does what you tell it to do.
2.) String characters indexing start at 0
3.) Java also has a convenient for loop.
4.) You can wrap your code in code tags when posting it so it's easier to read.
Jul 10 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Lorin Leone | last post by:
Can anyone help me modify the program so that it recognizes strings like "Anna" as palindromes. To make the program "case-insensitive." using the built-in C++ function "toupper". and so that it...
23
by: Amar Prakash Tripaithi | last post by:
Dan Hoey, who had recently graduated, wrote a C program to look for and construct the following beauty: A man, a plan, a caret, a ban, a myriad, a sum, a lac, a liar, a hoop, a pint, a catalpa,...
32
by: ramakrishnadeepak | last post by:
HI Everybody, I 've to submit a program on c.Can any one help me plz.........The problem is like this:: Write a program which computes the largest palindrome substring of a string. Input:...
4
by: outofmymind | last post by:
hi, im trying to solve the following question: Create a class responsible for determining whether a string is a palindrome. Show your test cases. Palindome mypal("bob"); ...
3
by: colinNeedsJavaHelp | last post by:
I am still having an exceptional amount of trouble with java. This is my new assignment, if anyone can help I would greatly appreciate it. I don't even know where to start. A word or phrase in...
20
by: Wabz | last post by:
Hello mates, Does anyone know how to write a function that tests if an integer is a palindrome in C language?
4
by: vicestner | last post by:
Write a Java program that prints the longest palindrome in an input file to standard output. A palindrome is a string whose reverse is the same as the original. Ignore case, whitespace, and...
5
by: rubyhuang | last post by:
i'm a new perl learner, this is the first perl task i will do. please help me. The user can input a string, and then a script will check to see if the string is a palindrome or not, displaying the...
2
by: bigtd08 | last post by:
help writing this palindrome program for my c++ class. HERE WHAT THE CODE SHOULD BE LIKE. Write a program that takes a line of input from the keyboard and check to see if that line is a palindrome....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.