Random Array Error [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 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

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 + ','

Store the result in the variable [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
Store the result in the variable
How do I store the result of number % 4 = 0 in a variable?
Like this: number1 = (number2 % 4 = 0)
Not sure what do you want exactly. Here are some hints for you:
int quotient = divisor / divider;
int remainder = divisor % divider;
boolean isDivisibleByFour = number % 4 == 0; // true or false

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);

Remove random index from an arrayList [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 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));

Array index out of bound exception how to overcome [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
public void updateGoalPositions(Goal[][] goals)
{
int row=(goals.length-1);
int col=(goals[0].length-1);
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
{
if(goals[i][j+1].isHit())
{
Goal temp=goals[i][j+1];
goals[i][j+1]=goals[i][j];
goals[i][j]=temp;
}
else
if(goals[i][j-1].isHit())
{
Goal temp=goals[i][j-1];
goals[i][j-1]=goals[i][j];
goals[i][j]=temp;
}
}
}
goals[i][j-1] shows error. How to override it?
when j = 0, j - 1 gives -1 which causes the error so test condition only if j - 1 >= 0
else
if(j - 1 >= 0 && goals[i][j-1].isHit())
{
Goal temp=goals[i][j-1];
goals[i][j-1]=goals[i][j];
goals[i][j]=temp;
}
On first j-iteration, j=0 and j-1's value is -1
Add if (j-1>-1) for example
When i = 0 and j = 0, goals[i][j-1] is going to mean goals[0][-1] that is your issue, you need to fix your algorithm.
Everywhere you have j+1 you'll exceed the limit in the last iteration.
Why? Because arrays are zero-based in Java, meaning that if you have an array of size N, the indexes will run between [0, N-1]. In your last iteration, when j is N-1, j+1 is actually N, which is out of bounds.
Read about arrays in the JLS - Chapter 10. Arrays.
Note also that you have places where j-1 will be -1 (in the first iteration where j is 0).

Categories