Remove random index from an arrayList [closed] - java

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have an ArrayList of 4 items. I need to remove one item randomly and display the updated ArrayList. However my random keeps targeting the second and third element on the array list. As far as I understand my random would count like this: 0 1 2 3. Shouldn't that be enough to cover my 4 elements? Why does it keep targeting the same indexes? I have tried increasing the random number (4) + 1, but that puts me out of bounds.
Random rand = new Random();
Scanner input = new Scanner(System.in);
int numberOfGuests = 4;
ArrayList<String> guestList = new ArrayList<>(4);
System.out.println("Enter 4 guests:");
for(int i = 1; i <=numberOfGuests; i++){
System.out.printf("guest%d: ", i);
guestList.add(input.nextLine());
}
System.out.println("Guest List: " + guestList);
String remove = guestList.remove(rand.nextInt(4));
System.out.printf("%s can't come%n" , remove);
System.out.println("Guest List: " + guestList);

yoy check when you user random number generation the rage is set to length of array minus one such as in your case
String remove = guestList.remove(rand.nextInt(3));

Related

Java Random Double Array from -50 to 50 [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I need to populate an array with random numbers from -50 to 50 and need to use Math.random(). Here is my current code:
double[] randomNums = new double[5];
for (int i = 0; i < randomNums.length; i++) {
randomNums[i] = 100 * Math.random() - 50;
}
for (double i: randomNums) {
System.out.println(i + ',');
}
Output:
-5.836717454677796,
44.07635593282988,
23.650145270722884,
93.00810678750743,
54.0536237451922
Why is this going above 50?
You are adding the value of the char ,, which has value 44 to each double. You can test this using:
System.out.println(0 + ',');
Output: 44
To fix this you can simply remove the + ','

I want to get each array values separately for calculation [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I'm java beginner, This is my code.
int [] arr = {1,3,4,5,5,6};
I want to get each array values separately for calculation. can you please help me.
For example: I want first two digits (array index 0 and 1) only how to get that.
to get the first and second item, do like this:
int [] arr = {1,3,4,5,5,6};
System.out.println("pos 0: "+arr[0]+" pos 1: "+arr[1]);
by doing arr[n-1], you select the 'n'th value in the array.
now, if you want to multiply these first two values..:
int result = arr[0] * arr[1]; // 1 * 3
System.out.println(result) // 3
For example: I want to multiply first two digit (1*3) after that (4*5) like that.. so I want to get these values separately.
To do this,
int [] arr = {1,3,4,5,5,6};
int ans;
for(int i=0;i<6;i+=2){
ans = arr[i]*arr[i+1];
System.out.println(""+ans);
}

Do while loop nested in for loop wont iterate [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
I need to enter N which is a number of numbers for which I need to see if they can be divided by 3. Afterwards I need to display what % of numbers from the N I can divide. Numbers need to go from 15 to 62 and they need to loop until I enter the right value each time, but they don't. Instead, they just repeat the for loop regardless of my input. Here is the code:
System.out.println("Enter N number of numbers");
int N = TextIO.getlnInt();
int number;
int counterOfDivisible = 0;
for(int i = 0; i < N ; i++) {
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number<15 && number>62);
if(number%3==0)
counterOfDivisible++;
}
System.out.println("% of numbers from the N that can be divided by 3 is " + (counterOfDivisible*100.0)/N + "%");
number can never be <15 and >62 at the same time. Think about your condition.
Thank you all for the quick reply, first time here. Quite silly mistake on my behalf.
This seems to do the trick i was aiming for.
do {
System.out.println("Please enter a number from the 15-62 span");
number = TextIO.getlnInt();
} while (number < 15 || number > 62);

Random Array Error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Why does the program show java.lang.ArrayIndexOutOfBoundsException: 11?
The program needs to print 11 numbers. 9 numbers should be random.
It prints 11 numbers but it has an error.
public class Main {
public static void main(String[] args) {
Random random = new Random();
int[] Cnum = new int[11];
Cnum[0]=0;
Cnum[1]=9;
System.out.print(Cnum[0]);
System.out.print(Cnum[1]);
for(int a = 2; 2 < 10; a++){
Cnum[a]=random.nextInt(9);
System.out.print(Cnum[a]);
}
}
}
The output should be (x is random) :
09xxxxxxxxx
But the output it does is:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 11
at CellphoneNumberGenerator.Main.main(Main.java:17)
09505423220
for(int a=2;2<10;a++)
That is basically a while(True) (infinite) loop because 2 is always less than 10. Perhaps you meant:
for(int a= 2; a<10; a++)
In your for cycle the condition is wrong.
You have 2 < 10 instead of a < 10

Using the contains method [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The objective of this program is to take the two arrays x and y and then create and array list contain numbers that occur in both x and y and then to print out this array of matching numbers. When I go to compile it gives me the error class expected on line 19 and another error saying ";" expected on the same line which I'm guessing is due to the other error. Is it just an error on that line or is it a bigger issue?
import java.util.ArrayList;
import java.util.Arrays;
public class FindCommon {
public static void main (String[] args) {
ArrayList list = new ArrayList();
int[] x = {1, 4, 3, 0, 1, 2};
int[] y = {6, 4, 5, 0, 6, 1};
for (int i = 0; i < x.length ; i++){
int number = x[i];
if (y[].(contains(x[i])){ // Line 19
list.add(x[i]);
}
System.out.println(list);
}
}
}
if (y[].(contains(x[i])){
The above line is invalid for a various of reasons. The index for y array is missing. Also according to the requirement, you need to call the contains() method on the list and not the array. And there seems to be an extra pair of braces.
if (!list.contains(x[i])){
list.add(x[i]); // Add to the list if it doesn't contain the value already
}
You need to do the same for the elements of y too.
Better Solution: You can use a Set instead of a List. It'll eliminate the duplicates for you. All you need to do is just add elements to your set from both the arrays.

Categories