patternjavaMinor
Palindrome Checker
Viewed 0 times
palindromecheckerstackoverflow
Problem
I've just started learning Java and went to compare my finished product to some others on Stack Overflow. Is there a reason why mine is "simple" and the others seem ridiculously hard to even understand (for me)? Link to the other palindrome checker.
Can you just break it down for me (the good/bad/ugly of my code)? Constructive criticism would be great as I'm very good at memorizing the books I use to learn Java. I just have a hard time implementing them and it's always good to have a second opinion.
Can you just break it down for me (the good/bad/ugly of my code)? Constructive criticism would be great as I'm very good at memorizing the books I use to learn Java. I just have a hard time implementing them and it's always good to have a second opinion.
public class Checker
{
public static void main(String [] args) {
String original = "hannah";
String reversed = original;
for (String part : original.split(" ")) {
System.out.println(original);
System.out.println(new StringBuilder(part).reverse().toString());
}
System.out.print(original.equals(new StringBuilder(reversed).reverse().toString()));
}
}Solution
The most misleading part of the code is this:
Get in the habit of splitting meaningful chunks of work into functions whenever possible.
That's more readable now, isn't it?
String reversed = original; — reversed is not reversed at all! Let's get rid of that confusion right away. Also note a few minor points…public class Checker
{
public static void main(String[] args) {
// Consider using args instead of hard-coding your test case
String original = "hannah";
for (String part : original.split(" ")) {
// I think you meant to print part instead of original here...
System.out.println(part);
// System.out.println() implicitly calls .toString() on its argument
System.out.println(new StringBuilder(part).reverse());
}
System.out.print(original.equals(new StringBuilder(original).reverse().toString()));
}
}Get in the habit of splitting meaningful chunks of work into functions whenever possible.
public class Checker
{
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static boolean isPalindrome(String original) {
return original.equals(reverse(original));
}
public static void main(String[] args) {
String original = "hannah";
for (String word : original.split(" ")) {
System.out.println(word);
System.out.println(reverse(word));
}
System.out.print(isPalindrome(original));
}
}That's more readable now, isn't it?
Code Snippets
public class Checker
{
public static void main(String[] args) {
// Consider using args instead of hard-coding your test case
String original = "hannah";
for (String part : original.split(" ")) {
// I think you meant to print part instead of original here...
System.out.println(part);
// System.out.println() implicitly calls .toString() on its argument
System.out.println(new StringBuilder(part).reverse());
}
System.out.print(original.equals(new StringBuilder(original).reverse().toString()));
}
}public class Checker
{
private static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static boolean isPalindrome(String original) {
return original.equals(reverse(original));
}
public static void main(String[] args) {
String original = "hannah";
for (String word : original.split(" ")) {
System.out.println(word);
System.out.println(reverse(word));
}
System.out.print(isPalindrome(original));
}
}Context
StackExchange Code Review Q#31641, answer score: 4
Revisions (0)
No revisions yet.