Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to check for null value?

Reply
Thread Tools

How to check for null value?

 
 
Brett
Guest
Posts: n/a
 
      07-26-2003
I am working on a program where I need to check a very short string to
see if it is either one or two digits. Specifically I need to check if
there is a second digit or not.
I have tried:
if ( isNull(string someString.charAt(1)) )
if ( string someString.charAt(1) == null )
if ( isNaN(string someString.charAt(1)) )
if ( string someString.length < 2 )

I know that some of these are wrong for a string object (or all are
wrong) but all of these give "cannot resolve symbol" compiler errors.
This seems like it should be a very simple thing to but I not finding a
soultion that can do this. Any help appreciated.

Thanks,
Brett

 
Reply With Quote
 
 
 
 
Thomas Kellerer
Guest
Posts: n/a
 
      07-26-2003
Brett schrieb:
> I am working on a program where I need to check a very short string to
> see if it is either one or two digits. Specifically I need to check if
> there is a second digit or not.
> I have tried:
> if ( isNull(string someString.charAt(1)) )
> if ( string someString.charAt(1) == null )
> if ( isNaN(string someString.charAt(1)) )
> if ( string someString.length < 2 )
>
> I know that some of these are wrong for a string object (or all are
> wrong) but all of these give "cannot resolve symbol" compiler errors.
> This seems like it should be a very simple thing to but I not finding a
> soultion that can do this. Any help appreciated.
>
> Thanks,
> Brett
>


You should really go back and read your Java tutorial again

if (someString != null)
{
int len = someString().trim().length();
switch (len)
{
case 1: // one digit string
break;
case 2: // two digit string
break;
default:
// do something else
}
}

Thomas

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      07-26-2003
On Sat, 26 Jul 2003 17:12:35 GMT, Brett <(E-Mail Removed)> wrote
or quoted :

>I am working on a program where I need to check a very short string to
>see if it is either one or two digits. Specifically I need to check if
>there is a second digit or not.
> I have tried:
>if ( isNull(string someString.charAt(1)) )
>if ( string someString.charAt(1) == null )
>if ( isNaN(string someString.charAt(1)) )
>if ( string someString.length < 2 )


Your syntax is quite imaginative. Usually for something like this you
can find an example in a text book or online tutorial.


if ( something == null || something.length() < 2 )

Your attempts indicate you have quite a few misconceptions about Java.

1. you can't just make up method names. They must be defined, or you
must write them, e.g. isNull


2. String is a class and must always be specified by a capital letter.

3. when defining a variable a you mention its class, e.g. String, but
you may not when you reference it, e.g. in an IF statement.

4. the notion of NaN applies only to floating point numbers, not
Strings. See http://mindprod.com/jgloss/floatingpoint.html

5. charAt treats the String as a char[]. If you go out of bounds you
get an exception, not a null result.

6. In Java sometimes you write length and sometimes you write
length(), and sometimes size(). This is done to haze newbies and
boost billable hours correcting the errors.



--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      07-27-2003
> You should really go back and read your Java tutorial again
>
> if (someString != null)
> {
> int len = someString().trim().length();
> switch (len)
> {
> case 1: // one digit string
> break;
> case 2: // two digit string
> break;
> default:
> // do something else
> }
> }
>
> Thomas
>


I don't know what tutorials you've used but I own several books and I
have never seen a series of 3 methods in row like that before. I will
give it a shot.

thanks,
Brett


 
Reply With Quote
 
Kabal
Guest
Posts: n/a
 
      07-27-2003
public boolean validateString(String someString) {
if (someString != null && someString.length() >= 2) {
try {
int n = Integer.parseInt(someString);

if (n > 9) {
return true;
}
} catch (Exception ex) {
return false;
}
}

return false;
}

Brett <(E-Mail Removed)> wrote in message news:<7EyUa.169283$(E-Mail Removed) v.net>...
> I am working on a program where I need to check a very short string to
> see if it is either one or two digits. Specifically I need to check if
> there is a second digit or not.
> I have tried:
> if ( isNull(string someString.charAt(1)) )
> if ( string someString.charAt(1) == null )
> if ( isNaN(string someString.charAt(1)) )
> if ( string someString.length < 2 )
>
> I know that some of these are wrong for a string object (or all are
> wrong) but all of these give "cannot resolve symbol" compiler errors.
> This seems like it should be a very simple thing to but I not finding a
> soultion that can do this. Any help appreciated.
>
> Thanks,
> Brett

 
Reply With Quote
 
Tom Davies
Guest
Posts: n/a
 
      07-27-2003


Brett wrote:
>> You should really go back and read your Java tutorial again
>>
>> if (someString != null)
>> {
>> int len = someString().trim().length();
>> switch (len)
>> {
>> case 1: // one digit string
>> break;
>> case 2: // two digit string
>> break;
>> default:
>> // do something else
>> }
>> }
>>
>> Thomas
>>

>
> I don't know what tutorials you've used but I own several books and I
> have never seen a series of 3 methods in row like that before. I will
> give it a shot.


Try someString.trim().length();

 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      07-27-2003
Roger wrote:
> Please have pity on him, I think he is an ex VB programmer, and VB
> really ties itself in knots with it's nulls and variants that contain
> Nothing!
>


Well, although that would make me sound less stupid I am unfortunately
new to programming period I only have half a clue because I use Linux
which always needs some form of minor programming manipulation to get
things going in the right direction.

Brett

 
Reply With Quote
 
A Bag Of Memes
Guest
Posts: n/a
 
      07-27-2003

"Brett" <(E-Mail Removed)> wrote in message
news:VGKUa.184338$(E-Mail Removed) .net...
> Roedy Green wrote:
> > Your syntax is quite imaginative. Usually for something like this you
> > can find an example in a text book or online tutorial.

>
> This comes from programing out of sheer determination, especially when I
> can't locate the info I need


I recommend going through Sun's tutorial. It's online at their site and
will introduce you to many of the concepts you'll need.

>
> > if ( something == null || something.length() < 2 )
> >
> > Your attempts indicate you have quite a few misconceptions about Java.
> >
> > 1. you can't just make up method names. They must be defined, or you
> > must write them, e.g. isNull

>
> From what I have read online isNull is a Java method from what I found
> searching "isNull Java" in Google. Unless it is some sort of frequently
> used custom method.


Java doesn't have methods (other than "public static void main(String[]
args)"). Classes and objects have methods, and they are defined as part of
each class.




 
Reply With Quote
 
Brett
Guest
Posts: n/a
 
      07-27-2003
> In this particular
> case, why not just use the simple
>
> something == null
>
> comparison? Or is the isNull method supposedly doing anything else?
>
> Regards,
> Marco


Thats what I thought too. But for some reason it isn't that simple. I
originaly tried:

String someString;
if (someString.charAt(1) == null)
doThis();

which is aparently incorrect usage because the compiler would give:
operator == cannot be applied to char,<nulltype>

But I did have great success using:
if (someString.trim().length() < 2)

Thanks for all the help guys this newsgroup rocks.
Brett

 
Reply With Quote
 
Frank D. Greco
Guest
Posts: n/a
 
      07-27-2003
Brett <(E-Mail Removed)> sez:

>Roger wrote:
>> Please have pity on him, I think he is an ex VB programmer, and VB
>> really ties itself in knots with it's nulls and variants that contain
>> Nothing!
>>

>
>Well, although that would make me sound less stupid I am unfortunately
>new to programming period I only have half a clue because I use Linux
>which always needs some form of minor programming manipulation to get
>things going in the right direction.


Brett,

There's a new beginner Java book from O'Reilly called
"Head First Java" that our user group just received for evaluation.
Looks useful for raw beginners:
http://www.oreilly.com/catalog/hfjava/

Try these too:
http://www.oreilly.com/catalog/javacook/
http://www.oreilly.com/catalog/javanut4/

I'd also recommend checking out Pat Niemeryer's "Learning Java".
http://www.oreilly.com/catalog/learnjava2/. Its excellent,
but assumes you are somewhat of a programmer already.

Frank G.
- NYJavaSIG Chair
+=========================================+
| Crossroads Technologies Inc. |
| Enterprise Java Engineering |
| Web: www.CrossroadsTech dot com |
| Email: fgreco @ crossroadstech dot com |
+=========================================+
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
To check or not to check for NULL? Mike Java 12 09-26-2008 10:46 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
"stringObj == null" vs "stringObj.equals(null)", for null check?? qazmlp1209@rediffmail.com Java 5 03-29-2006 10:37 PM
VB.NET Null to SQL Null (ASP.NET 2.0 GridView) Kivak Wolf ASP .Net 2 06-28-2005 02:01 PM
is (!ptr) or (ptr) valid way to check for NULL or NOT NULL? G Fernandes C Programming 9 02-27-2005 03:07 AM



Advertisments