I am making a small project for computer science. I am making a code to test if a string input is a palindrome, or a phrase that is the same forward and backwards, Ex. "racecar" = "racecar" when read backwards.
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter a palindrome or something...: ");
String myLine = keyboard.nextLine();
Palindrome test = new Palindrome();
public void setPaly(String setp)
in = setp;
boolean b1 = true;
int x = 0;
b1 = Character.isLetter(x);
if (b1 != false)
paly = paly + in.substring(x, x+1);
This is a very simple problem, perhaps even a simple typo. Your line
b1 = Character.isLetter(x)
is, I presume, attempting to check if the value at x in the string is a character. However, it does not do that, for a few reasons.
x is an int. If we check the docs we find the following:
isLetter(int codePoint) Determines if the specified character (Unicode code point) is a letter.
When you pass isLetter the int (x, which will count from 0 to some reasonably low value), it simply checks if those code points are letters. The first Unicode letter is at U+0041, and indeed if you enter a sting longer than 41 characters into your program, it'll start returning values.
I imagine what you are looking for is
b1 = Character.isLetter(in.charAt(x));
This is a simple conflation of index values with values at an index, a problem that would be normally caught precompile, but isn't because of the extra methods of isLetter.
Because it never returns as the codepoint being a letter (until you get past the initial control characters and gubbins), the condition is simply never true, and thus doesn't modify paly.