Searching for a String in an Array and returning the position












0















Hi I'm a complete newbie on programming and I try to search for a certain String in an array. When it's found the method should return the index but if the String is not found it should return -1.



public int poitionOfWord(String testWord) {
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i].equals(testWord)) {
return i;
}
}
return -1;
}


would this method return always -1 or would it actually terminate when finding a word and would return i.










share|improve this question




















  • 2





    What is the value of testWord passed and what are the contents of wordArray

    – user7
    Nov 23 '18 at 15:13











  • It will terminate as soon as there's a match, i.e. it will short-circuit.

    – Federico Peralta Schaffner
    Nov 23 '18 at 15:16











  • You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

    – deHaar
    Nov 23 '18 at 15:16











  • Your code looks good. One way of answering your question is running your code...

    – f1sh
    Nov 23 '18 at 15:22











  • Your code is fine. It will return the position of the item if found or -1 if not found.

    – forpas
    Nov 23 '18 at 15:29
















0















Hi I'm a complete newbie on programming and I try to search for a certain String in an array. When it's found the method should return the index but if the String is not found it should return -1.



public int poitionOfWord(String testWord) {
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i].equals(testWord)) {
return i;
}
}
return -1;
}


would this method return always -1 or would it actually terminate when finding a word and would return i.










share|improve this question




















  • 2





    What is the value of testWord passed and what are the contents of wordArray

    – user7
    Nov 23 '18 at 15:13











  • It will terminate as soon as there's a match, i.e. it will short-circuit.

    – Federico Peralta Schaffner
    Nov 23 '18 at 15:16











  • You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

    – deHaar
    Nov 23 '18 at 15:16











  • Your code looks good. One way of answering your question is running your code...

    – f1sh
    Nov 23 '18 at 15:22











  • Your code is fine. It will return the position of the item if found or -1 if not found.

    – forpas
    Nov 23 '18 at 15:29














0












0








0








Hi I'm a complete newbie on programming and I try to search for a certain String in an array. When it's found the method should return the index but if the String is not found it should return -1.



public int poitionOfWord(String testWord) {
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i].equals(testWord)) {
return i;
}
}
return -1;
}


would this method return always -1 or would it actually terminate when finding a word and would return i.










share|improve this question
















Hi I'm a complete newbie on programming and I try to search for a certain String in an array. When it's found the method should return the index but if the String is not found it should return -1.



public int poitionOfWord(String testWord) {
for (int i = 0; i < wordArray.length; i++) {
if (wordArray[i].equals(testWord)) {
return i;
}
}
return -1;
}


would this method return always -1 or would it actually terminate when finding a word and would return i.







java arrays equals






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 '18 at 15:14









deHaar

2,45251628




2,45251628










asked Nov 23 '18 at 15:13









superomasuperoma

111




111








  • 2





    What is the value of testWord passed and what are the contents of wordArray

    – user7
    Nov 23 '18 at 15:13











  • It will terminate as soon as there's a match, i.e. it will short-circuit.

    – Federico Peralta Schaffner
    Nov 23 '18 at 15:16











  • You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

    – deHaar
    Nov 23 '18 at 15:16











  • Your code looks good. One way of answering your question is running your code...

    – f1sh
    Nov 23 '18 at 15:22











  • Your code is fine. It will return the position of the item if found or -1 if not found.

    – forpas
    Nov 23 '18 at 15:29














  • 2





    What is the value of testWord passed and what are the contents of wordArray

    – user7
    Nov 23 '18 at 15:13











  • It will terminate as soon as there's a match, i.e. it will short-circuit.

    – Federico Peralta Schaffner
    Nov 23 '18 at 15:16











  • You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

    – deHaar
    Nov 23 '18 at 15:16











  • Your code looks good. One way of answering your question is running your code...

    – f1sh
    Nov 23 '18 at 15:22











  • Your code is fine. It will return the position of the item if found or -1 if not found.

    – forpas
    Nov 23 '18 at 15:29








2




2





What is the value of testWord passed and what are the contents of wordArray

– user7
Nov 23 '18 at 15:13





What is the value of testWord passed and what are the contents of wordArray

– user7
Nov 23 '18 at 15:13













It will terminate as soon as there's a match, i.e. it will short-circuit.

– Federico Peralta Schaffner
Nov 23 '18 at 15:16





It will terminate as soon as there's a match, i.e. it will short-circuit.

– Federico Peralta Schaffner
Nov 23 '18 at 15:16













You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

– deHaar
Nov 23 '18 at 15:16





You have to make sure the content of testWord is actually stored in wordArray on an arbitrary position. Otherwise, you code looks ok and should meet the requirements.

– deHaar
Nov 23 '18 at 15:16













Your code looks good. One way of answering your question is running your code...

– f1sh
Nov 23 '18 at 15:22





Your code looks good. One way of answering your question is running your code...

– f1sh
Nov 23 '18 at 15:22













Your code is fine. It will return the position of the item if found or -1 if not found.

– forpas
Nov 23 '18 at 15:29





Your code is fine. It will return the position of the item if found or -1 if not found.

– forpas
Nov 23 '18 at 15:29












2 Answers
2






active

oldest

votes


















-1














Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.



Just to make code crisp and concise, you can use something like this,



public static String wordArray = new String{"a", "b"};
public static int poitionOfWord(String testWord) {
return Arrays.asList(wordArray).indexOf(testWord);
}


Then test it with some code,



public static void main(String args) {
System.out.println(poitionOfWord("a"));
System.out.println(poitionOfWord("z"));
}


This prints,



1
-1





share|improve this answer

































    -1














    In general, when your function reaches a return statement, it will terminate and return the given value.






    share|improve this answer























      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%2f53449102%2fsearching-for-a-string-in-an-array-and-returning-the-position%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      -1














      Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.



      Just to make code crisp and concise, you can use something like this,



      public static String wordArray = new String{"a", "b"};
      public static int poitionOfWord(String testWord) {
      return Arrays.asList(wordArray).indexOf(testWord);
      }


      Then test it with some code,



      public static void main(String args) {
      System.out.println(poitionOfWord("a"));
      System.out.println(poitionOfWord("z"));
      }


      This prints,



      1
      -1





      share|improve this answer






























        -1














        Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.



        Just to make code crisp and concise, you can use something like this,



        public static String wordArray = new String{"a", "b"};
        public static int poitionOfWord(String testWord) {
        return Arrays.asList(wordArray).indexOf(testWord);
        }


        Then test it with some code,



        public static void main(String args) {
        System.out.println(poitionOfWord("a"));
        System.out.println(poitionOfWord("z"));
        }


        This prints,



        1
        -1





        share|improve this answer




























          -1












          -1








          -1







          Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.



          Just to make code crisp and concise, you can use something like this,



          public static String wordArray = new String{"a", "b"};
          public static int poitionOfWord(String testWord) {
          return Arrays.asList(wordArray).indexOf(testWord);
          }


          Then test it with some code,



          public static void main(String args) {
          System.out.println(poitionOfWord("a"));
          System.out.println(poitionOfWord("z"));
          }


          This prints,



          1
          -1





          share|improve this answer















          Your method is correct and it will return the index in case it finds a match else if it doesn't find the match, it will come out of loop and return -1.



          Just to make code crisp and concise, you can use something like this,



          public static String wordArray = new String{"a", "b"};
          public static int poitionOfWord(String testWord) {
          return Arrays.asList(wordArray).indexOf(testWord);
          }


          Then test it with some code,



          public static void main(String args) {
          System.out.println(poitionOfWord("a"));
          System.out.println(poitionOfWord("z"));
          }


          This prints,



          1
          -1






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 23 '18 at 15:28

























          answered Nov 23 '18 at 15:22









          Pushpesh Kumar RajwanshiPushpesh Kumar Rajwanshi

          7,8362927




          7,8362927

























              -1














              In general, when your function reaches a return statement, it will terminate and return the given value.






              share|improve this answer




























                -1














                In general, when your function reaches a return statement, it will terminate and return the given value.






                share|improve this answer


























                  -1












                  -1








                  -1







                  In general, when your function reaches a return statement, it will terminate and return the given value.






                  share|improve this answer













                  In general, when your function reaches a return statement, it will terminate and return the given value.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 23 '18 at 15:24









                  DrNaughtyDogDrNaughtyDog

                  83




                  83






























                      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%2f53449102%2fsearching-for-a-string-in-an-array-and-returning-the-position%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

                      Wiesbaden

                      To store a contact into the json file from server.js file using a class in NodeJS

                      Marschland