Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    42
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Question 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;
                }            
            }
    The code is very bad.

    Would somebody please help me?

    Thank you.

  • #2
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    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.

  • #3
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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;
    Trinithis

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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;
    Trinithis


  •  

    Posting Permissions

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