How to check for null value?

Discussion in 'Java' started by Brett, Jul 26, 2003.

  1. Brett

    Brett Guest

    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
    Brett, Jul 26, 2003
    #1
    1. Advertising

  2. 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
    Thomas Kellerer, Jul 26, 2003
    #2
    1. Advertising

  3. Brett

    Roedy Green Guest

    On Sat, 26 Jul 2003 17:12:35 GMT, Brett <> 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.
    Roedy Green, Jul 26, 2003
    #3
  4. Brett

    Brett Guest

    > 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
    Brett, Jul 27, 2003
    #4
  5. Brett

    Kabal Guest

    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 <> wrote in message news:<7EyUa.169283$>...
    > 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
    Kabal, Jul 27, 2003
    #5
  6. Brett

    Tom Davies Guest

    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();
    Tom Davies, Jul 27, 2003
    #6
  7. Brett

    Brett Guest

    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
    Brett, Jul 27, 2003
    #7
  8. "Brett" <> wrote in message
    news:VGKUa.184338$...
    > 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.
    A Bag Of Memes, Jul 27, 2003
    #8
  9. Brett

    Brett Guest

    > 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
    Brett, Jul 27, 2003
    #9
  10. Brett <> 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 |
    +=========================================+
    Frank D. Greco, Jul 27, 2003
    #10
  11. Brett wrote:

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


    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

    --
    Manish Jethani (manish.j at gmx.net)
    phone (work) +91-80-51073488
    Manish Jethani, Jul 27, 2003
    #11
  12. Brett

    Jacob Guest

    Thomas Kellerer wrote:
    > 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?
    Jacob, Jul 29, 2003
    #12
  13. Brett

    Tukla Ratte Guest

    On Tue, 29 Jul 2003 23:02:23 GMT, "Miguel De Anda"
    <_sodamnmad_@_hotmail_._com_> wrote:

    < snip >

    > 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?

    --
    Tukla, Squeaker of Chew Toys
    Official Mascot of Alt.Atheism
    Tukla Ratte, Jul 30, 2003
    #13
  14. Brett

    pete kirkham Guest

    Tukla Ratte wrote:
    > On Tue, 29 Jul 2003 23:02:23 GMT, "Miguel De Anda"
    > <_sodamnmad_@_hotmail_._com_> wrote:
    >
    > < snip >
    >
    >>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?
    >


    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
    pete kirkham, Jul 30, 2003
    #14
  15. Brett

    Tukla Ratte Guest

    On Wed, 30 Jul 2003 18:48:03 +0100, pete kirkham
    <> wrote:

    > Tukla Ratte wrote:
    > > On Tue, 29 Jul 2003 23:02:23 GMT, "Miguel De Anda"
    > > <_sodamnmad_@_hotmail_._com_> wrote:


    < snip >

    > >>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?
    > >

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

    --
    Tukla, Squeaker of Chew Toys
    Official Mascot of Alt.Atheism
    Tukla Ratte, Jul 31, 2003
    #15
  16. Brett

    sumanvarma

    Joined:
    Mar 31, 2008
    Messages:
    2
    -----------------------------------------------------------------------
    All of the three methods exist in java.
    I think you are using an old version of java compiler.
    sumanvarma, Mar 31, 2008
    #16
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GS
    Replies:
    3
    Views:
    14,180
    Luke Dalessandro
    Jan 15, 2006
  2. Replies:
    5
    Views:
    26,476
    Mike Schilling
    Mar 29, 2006
  3. G Fernandes
    Replies:
    9
    Views:
    570
    DHOLLINGSWORTH2
    Feb 27, 2005
  4. Col
    Replies:
    1
    Views:
    607
    peter koch
    Apr 21, 2006
  5. Mike
    Replies:
    12
    Views:
    699
    Daniel Pitts
    Sep 26, 2008
Loading...

Share This Page