How to stop user from selecting the same number once more?











up vote
0
down vote

favorite












I have created an Array holding [24] data, and I assigned some information in each index. my problem is when I want to call the indexes using Scanner from the keyboard, let's say I called index[12] from the user, next time I call it I want it to say, u already selected that number, choose a different number so on so forth. basically, I shouldn't call the same index twice, what is the best thing to use.



your help is much needed.










share|improve this question









New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
    – Sid
    yesterday










  • My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
    – Ole V.V.
    yesterday















up vote
0
down vote

favorite












I have created an Array holding [24] data, and I assigned some information in each index. my problem is when I want to call the indexes using Scanner from the keyboard, let's say I called index[12] from the user, next time I call it I want it to say, u already selected that number, choose a different number so on so forth. basically, I shouldn't call the same index twice, what is the best thing to use.



your help is much needed.










share|improve this question









New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
















  • 1




    There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
    – Sid
    yesterday










  • My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
    – Ole V.V.
    yesterday













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have created an Array holding [24] data, and I assigned some information in each index. my problem is when I want to call the indexes using Scanner from the keyboard, let's say I called index[12] from the user, next time I call it I want it to say, u already selected that number, choose a different number so on so forth. basically, I shouldn't call the same index twice, what is the best thing to use.



your help is much needed.










share|improve this question









New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











I have created an Array holding [24] data, and I assigned some information in each index. my problem is when I want to call the indexes using Scanner from the keyboard, let's say I called index[12] from the user, next time I call it I want it to say, u already selected that number, choose a different number so on so forth. basically, I shouldn't call the same index twice, what is the best thing to use.



your help is much needed.







java arrays arraylist






share|improve this question









New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited yesterday









Ole V.V.

24.7k62349




24.7k62349






New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked yesterday









de santös

11




11




New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.








  • 1




    There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
    – Sid
    yesterday










  • My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
    – Ole V.V.
    yesterday














  • 1




    There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
    – Sid
    yesterday










  • My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
    – Ole V.V.
    yesterday








1




1




There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
– Sid
yesterday




There are a couple of good ideas. First is to store selections separately in another list/array and then querying that..
– Sid
yesterday












My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
– Ole V.V.
yesterday




My preferred solutions would depend on the circumstances. Would you want to give the context for this requirement?
– Ole V.V.
yesterday












3 Answers
3






active

oldest

votes

















up vote
2
down vote













Use a java.util.Set to store the selected indexed, for exmaple, java.util.HashSet.



It should look like:



Set<Integer> selected = new HashSet<>();

int userInput = ...; // get input from user

while (selected.contains(userInput)) {
// print u already selected that number, choose a different number so on so forth
userInput = ...; // get input from user
}


selected.add(userInput);

// do something with the index





share|improve this answer



















  • 1




    The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
    – runcoderun
    yesterday


















up vote
0
down vote













You must save the index of selected numbers, and then you make a comparison of all new numbers with your list's elements.



Scanner s = new Scanner (System.in);
int choice = s.nextInt();
List<Integer> choiced = new ArrayList<Integer>();


while (true) {//or your condition
label:
for (Integer i : choiced) {
if (choice == i) {
System.out.println("Index already selected, please select a different one");
break label;
}
}
choiced.add(choice);

choice = s.nextInt();
}





share|improve this answer








New contributor




Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















  • Why would you not just use the contains() method?
    – GBlodgett
    yesterday


















up vote
0
down vote













I tried both of your ways but still could solve it here is my code how would u have applied the codes that u guys wrote in here.



import java.util.Scanner;



public class Main {



public static void main(String args) {
Scanner in = new Scanner(System.in);
int number = 0;

String differentChocolate = new String[24];
differentChocolate[0] = "You receive: A star that weighs 7 grams";
differentChocolate[1] = "You receive: Praline Bag Assorted 800g";
differentChocolate[2] = "You receive: Kinder Surprise Santa 75g";
differentChocolate[3] = "You receive: Woolworths Christmas Chocolate Net Bag 72g";
differentChocolate[4] = "You receive: Quality Street Tub 726g";
differentChocolate[5] = "You receive: Cadbury Favourites Snowman Bowl 700g";
differentChocolate[6] = "You receive: Lindt Santa Pouch Bag 80g";
differentChocolate[7] = "You receive: Praline Bag Assorted 800g";




while (true){
System.out.println("Choose a chocolate (0-23): ");
number = in.nextInt();
System.out.println(differentChocolate[number]);
}
}
}





share|improve this answer








New contributor




de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.


















    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',
    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
    });


    }
    });






    de santös is a new contributor. Be nice, and check out our Code of Conduct.










     

    draft saved


    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238001%2fhow-to-stop-user-from-selecting-the-same-number-once-more%23new-answer', 'question_page');
    }
    );

    Post as a guest
































    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Use a java.util.Set to store the selected indexed, for exmaple, java.util.HashSet.



    It should look like:



    Set<Integer> selected = new HashSet<>();

    int userInput = ...; // get input from user

    while (selected.contains(userInput)) {
    // print u already selected that number, choose a different number so on so forth
    userInput = ...; // get input from user
    }


    selected.add(userInput);

    // do something with the index





    share|improve this answer



















    • 1




      The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
      – runcoderun
      yesterday















    up vote
    2
    down vote













    Use a java.util.Set to store the selected indexed, for exmaple, java.util.HashSet.



    It should look like:



    Set<Integer> selected = new HashSet<>();

    int userInput = ...; // get input from user

    while (selected.contains(userInput)) {
    // print u already selected that number, choose a different number so on so forth
    userInput = ...; // get input from user
    }


    selected.add(userInput);

    // do something with the index





    share|improve this answer



















    • 1




      The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
      – runcoderun
      yesterday













    up vote
    2
    down vote










    up vote
    2
    down vote









    Use a java.util.Set to store the selected indexed, for exmaple, java.util.HashSet.



    It should look like:



    Set<Integer> selected = new HashSet<>();

    int userInput = ...; // get input from user

    while (selected.contains(userInput)) {
    // print u already selected that number, choose a different number so on so forth
    userInput = ...; // get input from user
    }


    selected.add(userInput);

    // do something with the index





    share|improve this answer














    Use a java.util.Set to store the selected indexed, for exmaple, java.util.HashSet.



    It should look like:



    Set<Integer> selected = new HashSet<>();

    int userInput = ...; // get input from user

    while (selected.contains(userInput)) {
    // print u already selected that number, choose a different number so on so forth
    userInput = ...; // get input from user
    }


    selected.add(userInput);

    // do something with the index






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited yesterday

























    answered yesterday









    孙兴斌

    15.3k41542




    15.3k41542








    • 1




      The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
      – runcoderun
      yesterday














    • 1




      The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
      – runcoderun
      yesterday








    1




    1




    The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
    – runcoderun
    yesterday




    The add() method itself returns false if the element is already present, so you can really just use that and remove contains()
    – runcoderun
    yesterday












    up vote
    0
    down vote













    You must save the index of selected numbers, and then you make a comparison of all new numbers with your list's elements.



    Scanner s = new Scanner (System.in);
    int choice = s.nextInt();
    List<Integer> choiced = new ArrayList<Integer>();


    while (true) {//or your condition
    label:
    for (Integer i : choiced) {
    if (choice == i) {
    System.out.println("Index already selected, please select a different one");
    break label;
    }
    }
    choiced.add(choice);

    choice = s.nextInt();
    }





    share|improve this answer








    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Why would you not just use the contains() method?
      – GBlodgett
      yesterday















    up vote
    0
    down vote













    You must save the index of selected numbers, and then you make a comparison of all new numbers with your list's elements.



    Scanner s = new Scanner (System.in);
    int choice = s.nextInt();
    List<Integer> choiced = new ArrayList<Integer>();


    while (true) {//or your condition
    label:
    for (Integer i : choiced) {
    if (choice == i) {
    System.out.println("Index already selected, please select a different one");
    break label;
    }
    }
    choiced.add(choice);

    choice = s.nextInt();
    }





    share|improve this answer








    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.


















    • Why would you not just use the contains() method?
      – GBlodgett
      yesterday













    up vote
    0
    down vote










    up vote
    0
    down vote









    You must save the index of selected numbers, and then you make a comparison of all new numbers with your list's elements.



    Scanner s = new Scanner (System.in);
    int choice = s.nextInt();
    List<Integer> choiced = new ArrayList<Integer>();


    while (true) {//or your condition
    label:
    for (Integer i : choiced) {
    if (choice == i) {
    System.out.println("Index already selected, please select a different one");
    break label;
    }
    }
    choiced.add(choice);

    choice = s.nextInt();
    }





    share|improve this answer








    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    You must save the index of selected numbers, and then you make a comparison of all new numbers with your list's elements.



    Scanner s = new Scanner (System.in);
    int choice = s.nextInt();
    List<Integer> choiced = new ArrayList<Integer>();


    while (true) {//or your condition
    label:
    for (Integer i : choiced) {
    if (choice == i) {
    System.out.println("Index already selected, please select a different one");
    break label;
    }
    }
    choiced.add(choice);

    choice = s.nextInt();
    }






    share|improve this answer








    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    share|improve this answer



    share|improve this answer






    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.









    answered yesterday









    Cesare Fischetti

    764




    764




    New contributor




    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.





    New contributor





    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






    Cesare Fischetti is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.












    • Why would you not just use the contains() method?
      – GBlodgett
      yesterday


















    • Why would you not just use the contains() method?
      – GBlodgett
      yesterday
















    Why would you not just use the contains() method?
    – GBlodgett
    yesterday




    Why would you not just use the contains() method?
    – GBlodgett
    yesterday










    up vote
    0
    down vote













    I tried both of your ways but still could solve it here is my code how would u have applied the codes that u guys wrote in here.



    import java.util.Scanner;



    public class Main {



    public static void main(String args) {
    Scanner in = new Scanner(System.in);
    int number = 0;

    String differentChocolate = new String[24];
    differentChocolate[0] = "You receive: A star that weighs 7 grams";
    differentChocolate[1] = "You receive: Praline Bag Assorted 800g";
    differentChocolate[2] = "You receive: Kinder Surprise Santa 75g";
    differentChocolate[3] = "You receive: Woolworths Christmas Chocolate Net Bag 72g";
    differentChocolate[4] = "You receive: Quality Street Tub 726g";
    differentChocolate[5] = "You receive: Cadbury Favourites Snowman Bowl 700g";
    differentChocolate[6] = "You receive: Lindt Santa Pouch Bag 80g";
    differentChocolate[7] = "You receive: Praline Bag Assorted 800g";




    while (true){
    System.out.println("Choose a chocolate (0-23): ");
    number = in.nextInt();
    System.out.println(differentChocolate[number]);
    }
    }
    }





    share|improve this answer








    New contributor




    de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.






















      up vote
      0
      down vote













      I tried both of your ways but still could solve it here is my code how would u have applied the codes that u guys wrote in here.



      import java.util.Scanner;



      public class Main {



      public static void main(String args) {
      Scanner in = new Scanner(System.in);
      int number = 0;

      String differentChocolate = new String[24];
      differentChocolate[0] = "You receive: A star that weighs 7 grams";
      differentChocolate[1] = "You receive: Praline Bag Assorted 800g";
      differentChocolate[2] = "You receive: Kinder Surprise Santa 75g";
      differentChocolate[3] = "You receive: Woolworths Christmas Chocolate Net Bag 72g";
      differentChocolate[4] = "You receive: Quality Street Tub 726g";
      differentChocolate[5] = "You receive: Cadbury Favourites Snowman Bowl 700g";
      differentChocolate[6] = "You receive: Lindt Santa Pouch Bag 80g";
      differentChocolate[7] = "You receive: Praline Bag Assorted 800g";




      while (true){
      System.out.println("Choose a chocolate (0-23): ");
      number = in.nextInt();
      System.out.println(differentChocolate[number]);
      }
      }
      }





      share|improve this answer








      New contributor




      de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















        up vote
        0
        down vote










        up vote
        0
        down vote









        I tried both of your ways but still could solve it here is my code how would u have applied the codes that u guys wrote in here.



        import java.util.Scanner;



        public class Main {



        public static void main(String args) {
        Scanner in = new Scanner(System.in);
        int number = 0;

        String differentChocolate = new String[24];
        differentChocolate[0] = "You receive: A star that weighs 7 grams";
        differentChocolate[1] = "You receive: Praline Bag Assorted 800g";
        differentChocolate[2] = "You receive: Kinder Surprise Santa 75g";
        differentChocolate[3] = "You receive: Woolworths Christmas Chocolate Net Bag 72g";
        differentChocolate[4] = "You receive: Quality Street Tub 726g";
        differentChocolate[5] = "You receive: Cadbury Favourites Snowman Bowl 700g";
        differentChocolate[6] = "You receive: Lindt Santa Pouch Bag 80g";
        differentChocolate[7] = "You receive: Praline Bag Assorted 800g";




        while (true){
        System.out.println("Choose a chocolate (0-23): ");
        number = in.nextInt();
        System.out.println(differentChocolate[number]);
        }
        }
        }





        share|improve this answer








        New contributor




        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        I tried both of your ways but still could solve it here is my code how would u have applied the codes that u guys wrote in here.



        import java.util.Scanner;



        public class Main {



        public static void main(String args) {
        Scanner in = new Scanner(System.in);
        int number = 0;

        String differentChocolate = new String[24];
        differentChocolate[0] = "You receive: A star that weighs 7 grams";
        differentChocolate[1] = "You receive: Praline Bag Assorted 800g";
        differentChocolate[2] = "You receive: Kinder Surprise Santa 75g";
        differentChocolate[3] = "You receive: Woolworths Christmas Chocolate Net Bag 72g";
        differentChocolate[4] = "You receive: Quality Street Tub 726g";
        differentChocolate[5] = "You receive: Cadbury Favourites Snowman Bowl 700g";
        differentChocolate[6] = "You receive: Lindt Santa Pouch Bag 80g";
        differentChocolate[7] = "You receive: Praline Bag Assorted 800g";




        while (true){
        System.out.println("Choose a chocolate (0-23): ");
        number = in.nextInt();
        System.out.println(differentChocolate[number]);
        }
        }
        }






        share|improve this answer








        New contributor




        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        share|improve this answer



        share|improve this answer






        New contributor




        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.









        answered 14 hours ago









        de santös

        11




        11




        New contributor




        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.





        New contributor





        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






        de santös is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
        Check out our Code of Conduct.






















            de santös is a new contributor. Be nice, and check out our Code of Conduct.










             

            draft saved


            draft discarded


















            de santös is a new contributor. Be nice, and check out our Code of Conduct.













            de santös is a new contributor. Be nice, and check out our Code of Conduct.












            de santös is a new contributor. Be nice, and check out our Code of Conduct.















             


            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53238001%2fhow-to-stop-user-from-selecting-the-same-number-once-more%23new-answer', 'question_page');
            }
            );

            Post as a guest




















































































            Popular posts from this blog

            Florida Star v. B. J. F.

            Danny Elfman

            Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues