Results 1 to 4 of 4
  1. #1
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default How do I do this.

    This a homework problem, but I am stuck, I don't know how to fix it - it just says the result is wrong.

    An array of Strings, names, has been declared and initialized. Write the statements needed to determine whether any of the the array elements are null or refer to the empty String. Set the variable hasEmpty to true if any elements are null or empty-- otherwise set it to false.

    My ans:

    hasEmpty = false;
    for(int i = 0; i < names.length; i++){
    if(names[i] == null){ hasEmpty = true;}

    }


    Can anyone help?

    Thanks!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    it just says the result is wrong.
    What does that mean? Do you have an error message that you could copy and post here???

    There is a problem in your code that it is only checking for a null value and not for the case where the String value = "" (an empty string). You need to add a test for that. Remember that a string variable is an object and that to access/test the value of an object you must use a method. Clue: Read the API doc for String to see what method(s) would test for equality.

  3. #3
    frasifrasi is offline Member
    Join Date
    Jun 2008
    Posts
    20
    Rep Power
    0

    Default

    Norm, that was very helpful, thank you. I was able fo fix it.

  4. #4
    Niveditha's Avatar
    Niveditha is offline Senior Member
    Join Date
    May 2008
    Posts
    307
    Rep Power
    7

    Default

    Mark it as SOLVED... :)
    To finish sooner, take your own time....
    Nivedithaaaa

Posting Permissions

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