How to check for null value?

B

Brett

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
 
T

Thomas Kellerer

Brett said:
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
 
R

Roedy Green

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.
 
B

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

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
 
K

Kabal

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;
}
 
T

Tom Davies

Brett said:
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();
 
B

Brett

Roger said:
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
 
A

A Bag Of Memes

Brett said:
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.
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.
 
B

Brett

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
 
F

Frank D. Greco

Brett said:
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 |
+=========================================+
 
M

Manish Jethani

Brett said:
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.

If you're new to programming, then I'd strongly suggest Python.
It's a great way to start programming. I like this:

http://www.ibiblio.org/obp/thinkCSpy/

And BTW this is how you do it in Python:

if someString is not None and len(someString) is 2:
# do your stuff here ;-)

-Manish
 
J

Jacob

Thomas said:
int len = someString().trim().length();

I see this occationally, and I dislike it.

If you want to find the length of a string
there should be very good and explicit
stated reasons to leave out certain characters
(in this case ascii-32 among others) based on
their position in the string (in this case at
the front or in the back), don't you think?
 
T

Tukla Ratte

On Tue, 29 Jul 2003 23:02:23 GMT, "Miguel De Anda"

My newsreader (outlook express) somehow left out the rest of the thread
but....


int len = someString().trim().length();


doesn't return the length of the string "someString" since
"someString.trim()" is a new String object.

But doesn't the chained length() method then return the length of the
new (temporary) String?
 
P

pete kirkham

Tukla said:
On Tue, 29 Jul 2003 23:02:23 GMT, "Miguel De Anda"




But doesn't the chained length() method then return the length of the
new (temporary) String?

Yes, so if someString = " ", then the length of someString is 3 but
the value of someString().trim().length() is 0. So len will not be the
length of someString.

It is very unlikely that you will care about the length of the trimmed
string but never want access to the trimmed string at any other point of
execution.

If you want to do whitespace elimination you do it before calling the
length dependant code and cache the result, rather than trimming the
string in possibly many locations, incurring performance penalities, and
making it a right royal pain to refactor when you realise you want the
code to work with whitespace preserved strings.


Pete
 
T

Tukla Ratte

Yes, so if someString = " ", then the length of someString is 3 but
the value of someString().trim().length() is 0. So len will not be the
length of someString.

It is very unlikely that you will care about the length of the trimmed
string but never want access to the trimmed string at any other point of
execution.

If you want to do whitespace elimination you do it before calling the
length dependant code and cache the result, rather than trimming the
string in possibly many locations, incurring performance penalities, and
making it a right royal pain to refactor when you realise you want the
code to work with whitespace preserved strings.

Oh, absolutely. I've written code just as you describe numerous times
(in COBOL, Pascal, & BASIC, granted, not Java). I just wanted to make
sure I wasn't misunderstanding how chained methods work.

Thanks for the advice.
 
Joined
Mar 31, 2008
Messages
2
Reaction score
0
Brett said:
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
-----------------------------------------------------------------------
All of the three methods exist in java.
I think you are using an old version of java compiler.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top