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 );
}
}
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.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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,...
|
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:...
|
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");
...
|
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...
|
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?
|
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...
|
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...
|
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....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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...
|
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...
|
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,...
| |