+ 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; } }
7 Respuestas
+ 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
+ 9
@Anahit I don't think u need more than one class for ur code
+ 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
+ 5
@Yerucham I just couldn't call a method from the main method. If you can help me, I will be thankful.
0
Take a look on these options:
https://code.sololearn.com/c8OLa6oPvk6V/?ref=app
https://code.sololearn.com/cKX1twtIeLb0/?ref=app
0
Here are my new tries, now using Python. The logic is the same:
https://code.sololearn.com/ckzKZUhvplvr/?ref=app
https://code.sololearn.com/cdb28fBq2f90/?ref=app