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 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts

    comparing integer type with null.

    I am looping through an integer array to check to see if it is empty or not but it won't let me compare the arrays values with the term null.

    So what do I do instead?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    primitive types cannot be null only object references can be compared with null
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If I recall correctly all int variables are set to zero on declaration. So

    int arr[] = new int[5];

    is the same as writing

    int a[] = { 0, 0, 0, 0, 0 };

    So if 0 is a valid value in your integer array you will probably need to assign it to something else (maybe -1).

    david_kw

  • #4
    New Coder
    Join Date
    Nov 2006
    Posts
    35
    Thanks
    0
    Thanked 0 Times in 0 Posts
    that really sucks. All real numbers are valid for me :S Guess I will have to implement something myself.

    Thanks.

  • #5
    Regular Coder Aradon's Avatar
    Join Date
    Jun 2005
    Location
    USA
    Posts
    734
    Thanks
    0
    Thanked 20 Times in 19 Posts
    You could do one of two things.

    You could use something like the maximum number for an integer or you could create a new class that holds an integer and use that instead. That way you can check for a null reference.
    "To iterate is human, to recurse divine." -L. Peter Deutsch

  • #6
    New Coder
    Join Date
    Feb 2006
    Location
    Tennessee
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you absolutely have to be able to compare to null as opposed to zero, just use the Integer wrapper class.

    Code:
    Integer[] intArray = new Integer[5];
    intArray[0] through intArray[4] will initialize as null instead of 0, since it's an object instead of a primitive. It'll eat more memory, but you'll gain the ability to use your null comparison.
    Last edited by ZJRT; 04-04-2007 at 07:50 PM.

  • #7
    Regular Coder
    Join Date
    Mar 2006
    Posts
    258
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Does it have to be an array of integers? You could create an array of Strings instead, and use a letter instead of null:
    Code:
    public static void main(String[] args){
             String[] number = new String[5];
             
             //give initial values to array:
             for(int i=0; i<5; i++){
                number[i] = "e";
             }
             //add whatever numbers you want, in the form of a string:
             number[0]="2";
             number[2]="3";
             
             //then do testing loop:
             for(int j=0; j<5; j++){
                if(number[j].equals("e")){
                   System.out.println("null");
                }
                else
                   System.out.println(number[j]);
             }
          }
    
    /*
     * Output:
     * 2
     * null
     * 3
     * null
     * null
     */
    And of course, when you need to use a number for whatever operation, just parse it to integer.
    Also, you would have to take care of invalid inputs.
    Last edited by daniel_g; 04-05-2007 at 04:37 AM.

  • #8
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by ZJRT View Post
    If you absolutely have to be able to compare to null as opposed to zero, just use the Integer wrapper class.

    Code:
    Integer[] intArray = new Integer[5];
    intArray[0] through intArray[4] will initialize as null instead of 0, since it's an object instead of a primitive. It'll eat more memory, but you'll gain the ability to use your null comparison.
    Hi ZJRT!

    it is a wonderful solution that i was literally searching out for !

    Hats off to your trick, ZJRT!!


  •  

    Posting Permissions

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