# Thread: Checking if something is equals to anything in an array

1. Member
Join Date
Mar 2010
Posts
1
Rep Power
0

## Checking if something is equals to anything in an array

My code so far
import java.util.Random;

Java Code:
```class Test{

public static void main(String[] args){
Random Attack = new Random();
int rud = 0;
int rod[]={2,5,4,3,6};
for(int counter=1; counter<=1; counter++){
rud=Attack.nextInt(10);
}
if(rud==rod[]){
System.out.println("You hit");
System.out.println(rud);
}else{
System.out.println("You missed");
System.out.println(rud);
}
}

}```
I am trying to check if rud is equals to anything in rod but it doesnt work any idea on how to solve it ?

2. Major logic flaw. Do it with pencil and paper and I'm sure you'll see it.

3. Originally Posted by Sapster
My code so far

Java Code:
`		if(rud==rod[]){`
I am trying to check if rud is equals to anything in rod but it doesnt work any idea on how to solve it ?
My crap-o-meter barfed on this line; when will an entire array ever equal one little single int value?

kind regards,

Jos

ps. my same crap-o-meter started to burp already on that useless loop a bit earlier in your code.

4. Your crap-o-meter is broken if it burps on crap!

5. Originally Posted by PhHein
Your crap-o-meter is broken if it burps on crap!
Yes, it's a very old crap-o-meter; it doesn't even react to my own code ;-)

kind regards,

Jos *bwuck!*

6. Member
Join Date
Mar 2010
Posts
20
Rep Power
0
IŽll write the solution for you, so you can check where you were wrong.

Java Code:
```class Test{

public static void main(String[] args){

Random Attack = new Random();
int rud = 0;
int rod[]={2,5,4,3,6};
int nTries = 5;

for(int counter=[B]0[/B]; counter<nTries; counter++){
rud=Attack.nextInt(10);

if(rud==rod[counter]){
System.out.println("You hit");
System.out.println(rud);
}else{
System.out.println("You missed");
System.out.println(rud);
}
}
}

}```

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•