Sequential search in Java










-2















Here I have learned to make programs in java about sequential searches. But in my program, I realized there was something that needed to be added or improved, but I was confused about how. Can anyone give me a suggestion, thank you.



example.



enter sentence : BOOK
enter letter : O
Letter O is in the index : 1 , 2


My program code.



public class sequentialSearch 

public static int sequential(String read, char target)
char arr = read.toCharArray();
for(int i = 0 ; i < arr.length ; i++)
if(arr[i] == target)
return i;


return 0;


public static void main(String args)
Scanner input = new Scanner(System.in);

System.out.print("enter sentence: : ");
String read = input.next();
System.out.print("enter letter : ");
char target = input.next(".").charAt(0);

System.out.print("Letter "+target+" is in the index – ");
System.out.println(sequetial(ars,target));











share|improve this question

















  • 4





    I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

    – Mad Physicist
    Nov 14 '18 at 18:14











  • My output can only display one index

    – m.arianand
    Nov 14 '18 at 18:17











  • You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

    – DmiN
    Nov 14 '18 at 18:20











  • You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

    – Mad Physicist
    Nov 14 '18 at 18:20











  • @m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

    – Rad
    Nov 14 '18 at 18:33















-2















Here I have learned to make programs in java about sequential searches. But in my program, I realized there was something that needed to be added or improved, but I was confused about how. Can anyone give me a suggestion, thank you.



example.



enter sentence : BOOK
enter letter : O
Letter O is in the index : 1 , 2


My program code.



public class sequentialSearch 

public static int sequential(String read, char target)
char arr = read.toCharArray();
for(int i = 0 ; i < arr.length ; i++)
if(arr[i] == target)
return i;


return 0;


public static void main(String args)
Scanner input = new Scanner(System.in);

System.out.print("enter sentence: : ");
String read = input.next();
System.out.print("enter letter : ");
char target = input.next(".").charAt(0);

System.out.print("Letter "+target+" is in the index – ");
System.out.println(sequetial(ars,target));











share|improve this question

















  • 4





    I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

    – Mad Physicist
    Nov 14 '18 at 18:14











  • My output can only display one index

    – m.arianand
    Nov 14 '18 at 18:17











  • You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

    – DmiN
    Nov 14 '18 at 18:20











  • You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

    – Mad Physicist
    Nov 14 '18 at 18:20











  • @m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

    – Rad
    Nov 14 '18 at 18:33













-2












-2








-2








Here I have learned to make programs in java about sequential searches. But in my program, I realized there was something that needed to be added or improved, but I was confused about how. Can anyone give me a suggestion, thank you.



example.



enter sentence : BOOK
enter letter : O
Letter O is in the index : 1 , 2


My program code.



public class sequentialSearch 

public static int sequential(String read, char target)
char arr = read.toCharArray();
for(int i = 0 ; i < arr.length ; i++)
if(arr[i] == target)
return i;


return 0;


public static void main(String args)
Scanner input = new Scanner(System.in);

System.out.print("enter sentence: : ");
String read = input.next();
System.out.print("enter letter : ");
char target = input.next(".").charAt(0);

System.out.print("Letter "+target+" is in the index – ");
System.out.println(sequetial(ars,target));











share|improve this question














Here I have learned to make programs in java about sequential searches. But in my program, I realized there was something that needed to be added or improved, but I was confused about how. Can anyone give me a suggestion, thank you.



example.



enter sentence : BOOK
enter letter : O
Letter O is in the index : 1 , 2


My program code.



public class sequentialSearch 

public static int sequential(String read, char target)
char arr = read.toCharArray();
for(int i = 0 ; i < arr.length ; i++)
if(arr[i] == target)
return i;


return 0;


public static void main(String args)
Scanner input = new Scanner(System.in);

System.out.print("enter sentence: : ");
String read = input.next();
System.out.print("enter letter : ");
char target = input.next(".").charAt(0);

System.out.print("Letter "+target+" is in the index – ");
System.out.println(sequetial(ars,target));








java sequential






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 18:11









m.arianandm.arianand

33




33







  • 4





    I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

    – Mad Physicist
    Nov 14 '18 at 18:14











  • My output can only display one index

    – m.arianand
    Nov 14 '18 at 18:17











  • You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

    – DmiN
    Nov 14 '18 at 18:20











  • You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

    – Mad Physicist
    Nov 14 '18 at 18:20











  • @m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

    – Rad
    Nov 14 '18 at 18:33












  • 4





    I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

    – Mad Physicist
    Nov 14 '18 at 18:14











  • My output can only display one index

    – m.arianand
    Nov 14 '18 at 18:17











  • You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

    – DmiN
    Nov 14 '18 at 18:20











  • You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

    – Mad Physicist
    Nov 14 '18 at 18:20











  • @m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

    – Rad
    Nov 14 '18 at 18:33







4




4





I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

– Mad Physicist
Nov 14 '18 at 18:14





I have no idea what you are asking. Did you get an error? If so, what? How did your output differ from the expected?

– Mad Physicist
Nov 14 '18 at 18:14













My output can only display one index

– m.arianand
Nov 14 '18 at 18:17





My output can only display one index

– m.arianand
Nov 14 '18 at 18:17













You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

– DmiN
Nov 14 '18 at 18:20





You scan your array for a match and return the first match and thereby break out of the control flow. you should store all matching indices in a collection.

– DmiN
Nov 14 '18 at 18:20













You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

– Mad Physicist
Nov 14 '18 at 18:20





You should edit the question by clicking on the "edit" button beneath it. A question should contain all relevant information.

– Mad Physicist
Nov 14 '18 at 18:20













@m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

– Rad
Nov 14 '18 at 18:33





@m.arianand first welcome to SO. Second, would you please re-read your post and put all the relevant information. Most importantly, you didn't ask any question. What is the question? "How can I improve it" is not a proper question for SO. You have to be specific, otherwise you get down-vote and most importantly no answer.

– Rad
Nov 14 '18 at 18:33












1 Answer
1






active

oldest

votes


















0














You use return i; when find first match. And return exits your loop and whole sequential() function. You either need to return array or list of indexes or replace return i with simply printing that index.






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%2f53306409%2fsequential-search-in-java%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You use return i; when find first match. And return exits your loop and whole sequential() function. You either need to return array or list of indexes or replace return i with simply printing that index.






    share|improve this answer



























      0














      You use return i; when find first match. And return exits your loop and whole sequential() function. You either need to return array or list of indexes or replace return i with simply printing that index.






      share|improve this answer

























        0












        0








        0







        You use return i; when find first match. And return exits your loop and whole sequential() function. You either need to return array or list of indexes or replace return i with simply printing that index.






        share|improve this answer













        You use return i; when find first match. And return exits your loop and whole sequential() function. You either need to return array or list of indexes or replace return i with simply printing that index.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 18:24









        IvanIvan

        5,2281921




        5,2281921





























            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%2f53306409%2fsequential-search-in-java%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







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3