Reputation: 1
Alright. I have looked through how to make palindromes, and it looks like using the reverse() method seems to be my best bet. However, in my code I have run into an error I do not understand.
import java.util.*;
public class retreiveInput
{
private Scanner input = new Scanner(System.in);
private int fives = 0;
public retreiveInput(){
fives = input.nextInt();
}
public void check()
{
while(fives < 9999 || fives > 100000)
{
System.out.println("The number does not work! It is NOT 5 digits!");
fives = input.nextInt();
}
String five = Integer.toString(fives);
five.equalsIgnoreCase(new StringBuilder(five).reverse().toString());
if(five = five.reverse()){
}
}
}
At the reverse in the code, it is giving me the following error. "The method reverse() is undefined for the type String"
Any idea how to fix this? What the program is supposed to be doing is that at the reverse() point of the if statement, the program should be checking if the value of five is equal to the reverse of five.
Upvotes: 0
Views: 2586
Reputation:
if(five = five.reverse())
In the above code five is a string. reverse is a method of StringBuilder(). You also have some errors present in your if statement. You want this
StringBuilder sbFive = new StringBuilder(five)
if (five.equals(sbFive.reverse().toString()))
Also note you can do this a bit faster.
The way to do it faster is to start on each end and meet in the middle during your comparison. it takes half the executions :)
Upvotes: 4