Why is my method returning false even though the conditions are true?How can I “delimit” an integer from a given string?Reverse array algorithmMethod override returns nullJava - Method executed prior to Default ConstructorHow can i insert multiple input in one line in 2dArray loopingI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?How can I print a pyramid of *Why does my method say it must return a type string and gives me an error even though my method does return a type string

Do high-wing aircraft represent more difficult engineering challenges than low-wing aircraft?

Is my test coverage up to snuff?

Holding rent money for my friend which amounts to over $10k?

How to describe a building set which is like LEGO without using the "LEGO" word?

Why can't I share a one use code with anyone else?

How to continually let my readers know what time it is in my story, in an organic way?

Geometric inspiration behind Hal's Wolf

I recently started my machine learning PhD and I have absolutely no idea what I'm doing

Using chord iii in a chord progression (major key)

Is random forest for regression a 'true' regression?

Were any of the books mentioned in this scene from the movie Hackers real?

Could a space colony 1g from the sun work?

Why would someone open a Netflix account using my Gmail address?

Understanding Deutch's Algorithm

How to rename multiple files in a directory at the same time

Assembly writer vs compiler

Capital gains on stocks sold to take initial investment off the table

Single word that parallels "Recent" when discussing the near future

What color to choose as "danger" if the main color of my app is red

How to redirect stdout to a file, and stdout+stderr to another one?

Will consteval functions allow template parameters dependent on function arguments?

Does the Rogue's Reliable Talent feature work for thieves' tools, since the rogue is proficient in them?

Why does SSL Labs now consider CBC suites weak?

How might a landlocked lake become a complete ecosystem?



Why is my method returning false even though the conditions are true?


How can I “delimit” an integer from a given string?Reverse array algorithmMethod override returns nullJava - Method executed prior to Default ConstructorHow can i insert multiple input in one line in 2dArray loopingI/P-a string S.O/P: For each digit start from 0-9,print count of their occurrence in S.Print10 lines,each line contain 2 space separated integersWhen use java regular-expression pattern.matcher(), source does not match regex.But, my hope result is ,source matches regexWould it make any difference giving arguments using scanner class instead of command line arguments?How can I print a pyramid of *Why does my method say it must return a type string and gives me an error even though my method does return a type string






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








1















public class Lab3Exercises {

public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));


public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;

if (reverseWord.equals(myWord))
return true;

else
return false;




I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false










share|improve this question
























  • Does this have anything to the lab utility?

    – Sid
    Mar 23 at 15:59











  • For your example word hannah, the value of reverseWord after the while loop is hhanna.

    – Abra
    Mar 23 at 16:31











  • You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

    – Mark Rotteveel
    Mar 23 at 16:54

















1















public class Lab3Exercises {

public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));


public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;

if (reverseWord.equals(myWord))
return true;

else
return false;




I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false










share|improve this question
























  • Does this have anything to the lab utility?

    – Sid
    Mar 23 at 15:59











  • For your example word hannah, the value of reverseWord after the while loop is hhanna.

    – Abra
    Mar 23 at 16:31











  • You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

    – Mark Rotteveel
    Mar 23 at 16:54













1












1








1








public class Lab3Exercises {

public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));


public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;

if (reverseWord.equals(myWord))
return true;

else
return false;




I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false










share|improve this question
















public class Lab3Exercises {

public static void main(String[] args)
Scanner s = new Scanner([System.in](https://System.in));
System.out.println("Please enter a word to check if it is a palindrome");
String myP = [s.next](https://s.next)();
System.out.println(palindrome(myP));


public static boolean palindrome(String p)
String myWord = p;
String reverseWord = "";
int i = 1;
while (i < myWord.length())
reverseWord = myWord.charAt(i) + myWord.substring(0, i) + myWord.substring((i + 1), myWord.length());
i++;

if (reverseWord.equals(myWord))
return true;

else
return false;




I checked and the reverseWord is the exact same as myWord yet it returns false everytime. I've tried hannah, racecar, etc. they all return false







java






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 23 at 15:59









Sid

3,237104085




3,237104085










asked Mar 23 at 15:14









Jessy SiaJessy Sia

853




853












  • Does this have anything to the lab utility?

    – Sid
    Mar 23 at 15:59











  • For your example word hannah, the value of reverseWord after the while loop is hhanna.

    – Abra
    Mar 23 at 16:31











  • You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

    – Mark Rotteveel
    Mar 23 at 16:54

















  • Does this have anything to the lab utility?

    – Sid
    Mar 23 at 15:59











  • For your example word hannah, the value of reverseWord after the while loop is hhanna.

    – Abra
    Mar 23 at 16:31











  • You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

    – Mark Rotteveel
    Mar 23 at 16:54
















Does this have anything to the lab utility?

– Sid
Mar 23 at 15:59





Does this have anything to the lab utility?

– Sid
Mar 23 at 15:59













For your example word hannah, the value of reverseWord after the while loop is hhanna.

– Abra
Mar 23 at 16:31





For your example word hannah, the value of reverseWord after the while loop is hhanna.

– Abra
Mar 23 at 16:31













You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

– Mark Rotteveel
Mar 23 at 16:54





You can simplify your code, return reverseWord.equals(myWord) is significantly shorter than that if-else you use...

– Mark Rotteveel
Mar 23 at 16:54












3 Answers
3






active

oldest

votes


















0














Let’s start from the simpler examples.



  • If p has length 1, for example s (a palindrome), you are setting reverseWord to the empty string. Since i starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal to s, the method returns false.

  • Input tt (another palindrome). You are running once through the loop, setting reverseWord to "t" + "t" + "" = "tt". The method correctly returns true in this case.

  • Input obo: You are setting reverseWord first to boo, then to "oob" ("o" + "ob" + ""), discarding the first value.

I think you need to think your logic through once more.



Tip: learn to use a debugger.






share|improve this answer






























    0














    You're condition isn't true because the way you build the reversed word is wrong.



    1. You're beginning with i equals to 0

    2. You reassign a new value to reverseWord in every loop

    3. The way you're building the reversed string is totally a mystery to me

    A good solution would be the following (you write the code :))



    1. Try beginning your loops with the index beginning at the very end of the original word

    2. Loop down until you reach 0, basically until no letter remains to be appended to reverseWord

    3. In every loop, append a single letter to reverseWord instead of the result of a complex operation





    share|improve this answer






























      -1














      • No need to make a new reference for the plaindrome word that you're going to check.

      • You should store the word in the reverse order in reverseWord variable.

      • Last index of a char array (String) in java is 1 less than string length. First index is 0.


      • So,the while loop read the string from the end to start index and store.



        public static boolean palindrome(String p)

        String reverseWord = "";
        int i = p.length() - 1;
        while (i >= 0)
        reverseWord += p.charAt(i);
        i--;


        if (reverseWord.equals(p))
        return true;


        return false;








      share|improve this answer




















      • 1





        Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

        – Ole V.V.
        Mar 23 at 16:12











      • Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

        – Harshana
        Mar 23 at 16:19












      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315192%2fwhy-is-my-method-returning-false-even-though-the-conditions-are-true%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Let’s start from the simpler examples.



      • If p has length 1, for example s (a palindrome), you are setting reverseWord to the empty string. Since i starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal to s, the method returns false.

      • Input tt (another palindrome). You are running once through the loop, setting reverseWord to "t" + "t" + "" = "tt". The method correctly returns true in this case.

      • Input obo: You are setting reverseWord first to boo, then to "oob" ("o" + "ob" + ""), discarding the first value.

      I think you need to think your logic through once more.



      Tip: learn to use a debugger.






      share|improve this answer



























        0














        Let’s start from the simpler examples.



        • If p has length 1, for example s (a palindrome), you are setting reverseWord to the empty string. Since i starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal to s, the method returns false.

        • Input tt (another palindrome). You are running once through the loop, setting reverseWord to "t" + "t" + "" = "tt". The method correctly returns true in this case.

        • Input obo: You are setting reverseWord first to boo, then to "oob" ("o" + "ob" + ""), discarding the first value.

        I think you need to think your logic through once more.



        Tip: learn to use a debugger.






        share|improve this answer

























          0












          0








          0







          Let’s start from the simpler examples.



          • If p has length 1, for example s (a palindrome), you are setting reverseWord to the empty string. Since i starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal to s, the method returns false.

          • Input tt (another palindrome). You are running once through the loop, setting reverseWord to "t" + "t" + "" = "tt". The method correctly returns true in this case.

          • Input obo: You are setting reverseWord first to boo, then to "oob" ("o" + "ob" + ""), discarding the first value.

          I think you need to think your logic through once more.



          Tip: learn to use a debugger.






          share|improve this answer













          Let’s start from the simpler examples.



          • If p has length 1, for example s (a palindrome), you are setting reverseWord to the empty string. Since i starts from 1, it is not less than the length, so the loop does not run. Since the empty string is not equal to s, the method returns false.

          • Input tt (another palindrome). You are running once through the loop, setting reverseWord to "t" + "t" + "" = "tt". The method correctly returns true in this case.

          • Input obo: You are setting reverseWord first to boo, then to "oob" ("o" + "ob" + ""), discarding the first value.

          I think you need to think your logic through once more.



          Tip: learn to use a debugger.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Mar 23 at 16:18









          Ole V.V.Ole V.V.

          33.2k74359




          33.2k74359























              0














              You're condition isn't true because the way you build the reversed word is wrong.



              1. You're beginning with i equals to 0

              2. You reassign a new value to reverseWord in every loop

              3. The way you're building the reversed string is totally a mystery to me

              A good solution would be the following (you write the code :))



              1. Try beginning your loops with the index beginning at the very end of the original word

              2. Loop down until you reach 0, basically until no letter remains to be appended to reverseWord

              3. In every loop, append a single letter to reverseWord instead of the result of a complex operation





              share|improve this answer



























                0














                You're condition isn't true because the way you build the reversed word is wrong.



                1. You're beginning with i equals to 0

                2. You reassign a new value to reverseWord in every loop

                3. The way you're building the reversed string is totally a mystery to me

                A good solution would be the following (you write the code :))



                1. Try beginning your loops with the index beginning at the very end of the original word

                2. Loop down until you reach 0, basically until no letter remains to be appended to reverseWord

                3. In every loop, append a single letter to reverseWord instead of the result of a complex operation





                share|improve this answer

























                  0












                  0








                  0







                  You're condition isn't true because the way you build the reversed word is wrong.



                  1. You're beginning with i equals to 0

                  2. You reassign a new value to reverseWord in every loop

                  3. The way you're building the reversed string is totally a mystery to me

                  A good solution would be the following (you write the code :))



                  1. Try beginning your loops with the index beginning at the very end of the original word

                  2. Loop down until you reach 0, basically until no letter remains to be appended to reverseWord

                  3. In every loop, append a single letter to reverseWord instead of the result of a complex operation





                  share|improve this answer













                  You're condition isn't true because the way you build the reversed word is wrong.



                  1. You're beginning with i equals to 0

                  2. You reassign a new value to reverseWord in every loop

                  3. The way you're building the reversed string is totally a mystery to me

                  A good solution would be the following (you write the code :))



                  1. Try beginning your loops with the index beginning at the very end of the original word

                  2. Loop down until you reach 0, basically until no letter remains to be appended to reverseWord

                  3. In every loop, append a single letter to reverseWord instead of the result of a complex operation






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 16:09









                  Yassin HajajYassin Hajaj

                  14.8k73062




                  14.8k73062





















                      -1














                      • No need to make a new reference for the plaindrome word that you're going to check.

                      • You should store the word in the reverse order in reverseWord variable.

                      • Last index of a char array (String) in java is 1 less than string length. First index is 0.


                      • So,the while loop read the string from the end to start index and store.



                        public static boolean palindrome(String p)

                        String reverseWord = "";
                        int i = p.length() - 1;
                        while (i >= 0)
                        reverseWord += p.charAt(i);
                        i--;


                        if (reverseWord.equals(p))
                        return true;


                        return false;








                      share|improve this answer




















                      • 1





                        Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                        – Ole V.V.
                        Mar 23 at 16:12











                      • Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                        – Harshana
                        Mar 23 at 16:19
















                      -1














                      • No need to make a new reference for the plaindrome word that you're going to check.

                      • You should store the word in the reverse order in reverseWord variable.

                      • Last index of a char array (String) in java is 1 less than string length. First index is 0.


                      • So,the while loop read the string from the end to start index and store.



                        public static boolean palindrome(String p)

                        String reverseWord = "";
                        int i = p.length() - 1;
                        while (i >= 0)
                        reverseWord += p.charAt(i);
                        i--;


                        if (reverseWord.equals(p))
                        return true;


                        return false;








                      share|improve this answer




















                      • 1





                        Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                        – Ole V.V.
                        Mar 23 at 16:12











                      • Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                        – Harshana
                        Mar 23 at 16:19














                      -1












                      -1








                      -1







                      • No need to make a new reference for the plaindrome word that you're going to check.

                      • You should store the word in the reverse order in reverseWord variable.

                      • Last index of a char array (String) in java is 1 less than string length. First index is 0.


                      • So,the while loop read the string from the end to start index and store.



                        public static boolean palindrome(String p)

                        String reverseWord = "";
                        int i = p.length() - 1;
                        while (i >= 0)
                        reverseWord += p.charAt(i);
                        i--;


                        if (reverseWord.equals(p))
                        return true;


                        return false;








                      share|improve this answer















                      • No need to make a new reference for the plaindrome word that you're going to check.

                      • You should store the word in the reverse order in reverseWord variable.

                      • Last index of a char array (String) in java is 1 less than string length. First index is 0.


                      • So,the while loop read the string from the end to start index and store.



                        public static boolean palindrome(String p)

                        String reverseWord = "";
                        int i = p.length() - 1;
                        while (i >= 0)
                        reverseWord += p.charAt(i);
                        i--;


                        if (reverseWord.equals(p))
                        return true;


                        return false;









                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 23 at 16:39

























                      answered Mar 23 at 16:12









                      HarshanaHarshana

                      357212




                      357212







                      • 1





                        Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                        – Ole V.V.
                        Mar 23 at 16:12











                      • Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                        – Harshana
                        Mar 23 at 16:19













                      • 1





                        Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                        – Ole V.V.
                        Mar 23 at 16:12











                      • Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                        – Harshana
                        Mar 23 at 16:19








                      1




                      1





                      Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                      – Ole V.V.
                      Mar 23 at 16:12





                      Too much spoon-feeding for my taste. Do you believe that the asker is learning anything from this?

                      – Ole V.V.
                      Mar 23 at 16:12













                      Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                      – Harshana
                      Mar 23 at 16:19






                      Updated the answer and anything else not clear in the answer?. You should practice to do the things in right way. Removed the false because if the condition satisfied nothing else going to execute in the code. Return statement should be the final statement of any java method.

                      – Harshana
                      Mar 23 at 16:19


















                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55315192%2fwhy-is-my-method-returning-false-even-though-the-conditions-are-true%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                      SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                      은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현