+ 4

What is wrong with my code? I am trying to check if the word is palindrome or not.

Hey! Can you help me with my code. I think my code checks rights , but doesn't return true, false (if the word is palindrome or not). public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String word = scanner.next(); Palindrome.checkifpalindrome(word); } } class Palindrome{ public static boolean checkifpalindrome(String word){ boolean isPalindrome = false; for(int i = 0; i < word.length()/2; ++i){ if(word.charAt(i) == word.charAt(word.length() - i - 1)) { isPalindrome = true; } } return isPalindrome; } }

12th Nov 2017, 4:42 PM
Anahit Harutyunyan
Anahit Harutyunyan - avatar
6 odpowiedzi
+ 3
You are checking, but you are returning just true or false. You aren't actually doing anything with this information. change - Palindrome.checkifpalindrome(word); To - System.out.println(Palindrome.checkifpalindrome(word)); This will print whatever is returned from your method. Nice code, keep it up
12th Nov 2017, 4:53 PM
LordHill
LordHill - avatar
+ 9
@Anahit I don't think u need more than one class for ur code
12th Nov 2017, 6:05 PM
David Akhihiero
David Akhihiero - avatar
+ 8
@Anahit because main method is static, it can only call static methods and only access static variables , to make a method callable from main make it a static method
12th Nov 2017, 7:55 PM
David Akhihiero
David Akhihiero - avatar
+ 5
@Yerucham I just couldn't call a method from the main method. If you can help me, I will be thankful.
12th Nov 2017, 6:21 PM
Anahit Harutyunyan
Anahit Harutyunyan - avatar
8th Dec 2017, 11:19 PM
Jonathan Álex
Jonathan Álex - avatar
9th Dec 2017, 11:30 AM
Jonathan Álex
Jonathan Álex - avatar