Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Comparing integer with array

1. ## Comparing integer with array

Hello..

I need to compare an integer value with array list.
Code:
```        if (year > 1986) {
Integer[] twelve = {1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986};
while (year != twelve) {
year -= 12;
}
}```

Thank you.

• I think you need to be more clear on what you are attempting to accomplish. Are you trying to see if the array contains the value of year? The way your code is set up from what I can see, you will probably end up in an infinite loop because an int value is never going to equal an array.

• This is what I think you were trying to do
Code:
```if(year > 1986) {
int[] years = {1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986};
boolean cont;
do {
cont = true;
for(int i = 0; i < years.length; ++i)
if(year == years[i])
cont = false;
if(cont)
year -= 12;
} while(cont);
}```
This is what you probably should do
Code:
```while(year > 1986)
year -= 12;```

• More along the lines of your original code:

Code:
```java.util.List<Integer> twelve = java.util.Arrays.asList(1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986);
if(year > 1986)
while(!twelve.contains(year))
year -= 12;```

•

#### Posting Permissions

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