Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-04-2007, 03:52 PM   PM User | #1
aatwo
New Coder

 
Join Date: Nov 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
aatwo is an unknown quantity at this point
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?
aatwo is offline   Reply With Quote
Old 04-04-2007, 04:59 PM   PM User | #2
shyam
Senior Coder

 
shyam's Avatar
 
Join Date: Jul 2005
Posts: 1,563
Thanks: 2
Thanked 163 Times in 160 Posts
shyam will become famous soon enough
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
shyam is offline   Reply With Quote
Old 04-04-2007, 05:04 PM   PM User | #3
david_kw
Senior Coder

 
Join Date: Nov 2006
Posts: 1,000
Thanks: 0
Thanked 0 Times in 0 Posts
david_kw will become famous soon enough
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
david_kw is offline   Reply With Quote
Old 04-04-2007, 05:36 PM   PM User | #4
aatwo
New Coder

 
Join Date: Nov 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
aatwo is an unknown quantity at this point
that really sucks. All real numbers are valid for me :S Guess I will have to implement something myself.

Thanks.
aatwo is offline   Reply With Quote
Old 04-04-2007, 05:46 PM   PM User | #5
Aradon
Moderator-san


 
Aradon's Avatar
 
Join Date: Jun 2005
Location: USA
Posts: 734
Thanks: 0
Thanked 20 Times in 19 Posts
Aradon is on a distinguished road
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
Aradon is offline   Reply With Quote
Old 04-04-2007, 07:42 PM   PM User | #6
ZJRT
New Coder

 
Join Date: Feb 2006
Location: Tennessee
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
ZJRT is an unknown quantity at this point
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..
ZJRT is offline   Reply With Quote
Old 04-05-2007, 03:56 AM   PM User | #7
daniel_g
Regular Coder

 
Join Date: Mar 2006
Posts: 258
Thanks: 1
Thanked 2 Times in 2 Posts
daniel_g is an unknown quantity at this point
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..
daniel_g is offline   Reply With Quote
Old 10-28-2008, 05:24 PM   PM User | #8
svisweswaran
New to the CF scene

 
Join Date: Oct 2008
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
svisweswaran is an unknown quantity at this point
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!!
svisweswaran is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:46 PM.


Advertisement
Log in to turn off these ads.