Reputation: 41
I've written code for my CS class to identify if a user inputted string is a palindrome or not. I've gotten the code to work. However, whenever I execute the code I get this message:
"Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: -1
at java.lang.String.charAt(String.java:658)
at palindrome.Palindrome.main(Palindrome.java:14)"
I think there's something wrong with my length
and i
but I'm not sure what. My code is below. I used aabbaa
for the user inputted string.
package palindrome;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println ("Enter A String:");
Scanner input = new Scanner (System.in);
String s = input.nextLine();
int length = s.length();
int i = 0;
while (length>= 0) {
if (s.charAt(i) == s.charAt(length-1)) {
i++;
length--;
if (length == i) {
System.out.println ("Your string is a palindrome");
length = 0;
}
}
else {
System.out.println ("Your string is not a palindrome");
length = 0;
}
}
}
}
Upvotes: 0
Views: 3649
Reputation: 1373
You should do some thing like this:
package palindrome;
import java.util.Scanner;
public class Palindrome {
public static void main(String[] args) {
System.out.println ("Enter A String:");
Scanner input = new Scanner (System.in);
String s = input.nextLine();
if ( s.equals(new StringBuffer(s).reverse().toString()) ){
System.out.println ("Your string is a palindrome");
else
System.out.println ("Your string is not a palindrome");
}
}
Upvotes: 0
Reputation: 137
When you iterate the while loop at that time value of length becomes negative, and array index starts from 0
thats why you getting the exception as StringIndexOutOfBoundsException
.
You should try this, it will works fine.
import java.util.Scanner;
public class Palindrome
{
public static void main(String[] args)
{
System.out.println("Enter A String:");
Scanner input = new Scanner(System.in);
String s = input.nextLine();
int length = s.length();
int i = 0;
while (length >= 0)
{
if (s.charAt(i) == s.charAt(length - 1))
{
i++;
length--;
if (length-1 == i)
{
System.out.println("Your string is a palindrome");
length = 0;
break;
}
}
else
{
System.out.println("Your string is not a palindrome");
length = 0;
break;
}
}
}
}
Upvotes: 0
Reputation: 7016
Just create a method like below and pass your string as argument to test whether string is a palindrome or not. Method will return true in case of string is palindrome
Idea is reverse your string and compare with original string if both are same then it is a palindrome.
public static boolean isPalindrome(String str) {
return str.equals(new StringBuffer().append(str).reverse().toString());
}
Upvotes: 1
Reputation: 25695
You write while(length >= 0) {}
and in the main
method you write length = 0;
trying to the loop.
That should be while(length > 0){}
Upvotes: 1
Reputation: 2833
When length equals 0, the first if-statement in your while loop tries to get s.charAt(0-1), i.e. s.charAt(-1), which is where the error occurs.
Perhaps try the following (not tested):
public static void main(String[] args) {
System.out.println ("Enter A String:");
Scanner input = new Scanner (System.in);
String s = input.nextLine();
int length = s.length();
int i = 0;
while (length > 0) {
if (s.charAt(i) == s.charAt(length-1)) {
i++;
length--;
if (length < 1) {
System.out.println ("Your string is a palindrome");
break;
}
}
else {
System.out.println ("Your string is not a palindrome");
break;
}
}
Upvotes: 2
Reputation: 15471
Your problem is that in every step you take two strides, but only decrease length by 1
So a trace: A user enters foooof
.
length = 6
i = 0
f == f
i = 1
length = 5
o == o
i = 2
length = 4
o == o
i = 3
length = 3 (You've now passed yourself)
o == o
i = 4
length = 2
length-i = -2 => index out of range
Upvotes: 0