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.
java arrays arraylist
New contributor
add a comment |
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.
java arrays arraylist
New contributor
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
add a comment |
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.
java arrays arraylist
New contributor
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
java arrays arraylist
New contributor
New contributor
edited yesterday
Ole V.V.
24.7k62349
24.7k62349
New contributor
asked yesterday
de santös
11
11
New contributor
New contributor
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
add a comment |
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
add a comment |
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
1
Theadd()
method itself returns false if the element is already present, so you can really just use that and removecontains()
– runcoderun
yesterday
add a comment |
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();
}
New contributor
Why would you not just use thecontains()
method?
– GBlodgett
yesterday
add a comment |
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]);
}
}
}
New contributor
add a comment |
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
1
Theadd()
method itself returns false if the element is already present, so you can really just use that and removecontains()
– runcoderun
yesterday
add a comment |
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
1
Theadd()
method itself returns false if the element is already present, so you can really just use that and removecontains()
– runcoderun
yesterday
add a comment |
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
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
edited yesterday
answered yesterday
孙兴斌
15.3k41542
15.3k41542
1
Theadd()
method itself returns false if the element is already present, so you can really just use that and removecontains()
– runcoderun
yesterday
add a comment |
1
Theadd()
method itself returns false if the element is already present, so you can really just use that and removecontains()
– 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
add a comment |
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();
}
New contributor
Why would you not just use thecontains()
method?
– GBlodgett
yesterday
add a comment |
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();
}
New contributor
Why would you not just use thecontains()
method?
– GBlodgett
yesterday
add a comment |
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();
}
New contributor
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();
}
New contributor
New contributor
answered yesterday
Cesare Fischetti
764
764
New contributor
New contributor
Why would you not just use thecontains()
method?
– GBlodgett
yesterday
add a comment |
Why would you not just use thecontains()
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
add a comment |
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]);
}
}
}
New contributor
add a comment |
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]);
}
}
}
New contributor
add a comment |
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]);
}
}
}
New contributor
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]);
}
}
}
New contributor
New contributor
answered 14 hours ago
de santös
11
11
New contributor
New contributor
add a comment |
add a comment |
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.
de santös is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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