472,779 Members | 2,726 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,779 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 2730
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: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth

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.