...

View Full Version : Comparing integer with array



frozenade
12-20-2007, 02:09 AM
Hello..

I need to compare an integer value with array list.

if (year > 1986) {
Integer[] twelve = {1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986};
while (year != twelve) {
year -= 12;
}
}
The code is very bad.

Would somebody please help me? :(

Thank you.

brad211987
12-21-2007, 03:49 PM
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.

Trinithis
12-24-2007, 01:38 AM
This is what I think you were trying to do


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


while(year > 1986)
year -= 12;

Trinithis
12-24-2007, 06:12 AM
More along the lines of your original 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;



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum