Results 1 to 14 of 14
  1. #1
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Array Test questions

    I am working on an assignment covering exception handling and am stuck on part of the assignment. How can you test for array length = 0?
    When I try something like: if (array.length == 0) on a null array, naturally I get a NullPointerException. I would try something like if (array != null) but both array length of 0 and null array are supposed to throw different expressions.

    I appreciate your help on my previous question and want to thank those who responded for being so polite and understanding of the issues facing a novice programmer.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Array Test questions

    Split the test in two parts:

    Java Code:
    if (array == null) throw new ...;
    if (array.length == 0) throw new ...;
    You fill in the dots.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Re: Array Test questions

    Sorry, that title should have been testing arrays, the current title makes it appear that I am looking or help on an exam rather than programing assistance.

  4. #4
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Re: Array Test questions

    Quote Originally Posted by JosAH View Post
    Split the test in two parts:

    Java Code:
    if (array.length == 0) throw new ...;
    You fill in the dots.

    kind regards,

    Jos
    That is what I tried initially, but it throws a NullPointerException.

    This is the code I currently have:

    Java Code:
            try{
                          
                 if (array.length == 0){
                    throw new BadArrayException("Array Length = 0");
                 }
    Exception in thread "main" java.lang.NullPointerException
    at testarea.TestArea.testArray(TestArea.java:48)
    at testarea.TestArea.main(TestArea.java:33)
    Java Result: 1

  5. #5
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Array Test questions

    You left out my first line of code ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  6. #6
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Re: Array Test questions

    That will catch the Null array, but will not catch array.length == 0, Is there a way to catch both?

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    14,423
    Blog Entries
    7
    Rep Power
    27

    Default Re: Array Test questions

    Quote Originally Posted by Doghouse View Post
    That will catch the Null array, but will not catch array.length == 0, Is there a way to catch both?
    Did you fill in the dots as I suggested? All you have to do (somewhere else) is catch the Exceptions thrown if the array happened to be null or having zero length ...

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  8. #8
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Re: Array Test questions

    Thank you for your help. I thought the execution stops when an exception is thrown. That is why I don't understand how this works.

    This is what I am working with at the moment.

    Java Code:
            try{
                if (array == null)
                    throw new BadArrayException("Array is Null");
                if (array.length == 0){
                    throw new BadArrayException("Array Length = 0");
                 }
            }
            catch (BadArrayException e)
                    {
                    System.out.println("Array is empty");
                    }
            }

  9. #9
    Doghouse is offline Member
    Join Date
    Mar 2015
    Posts
    10
    Rep Power
    0

    Default Re: Array Test questions

    I want to thank all of you for your help

  10. #10
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Array Test questions

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Array Test questions

    "Glad I found a place that focuses on being friendly to rookies. Have been slammed and shamed for asking simple questions on some other sites. "

    I hope that doesn't include this site as that would be totally uncalled for :/ A very odd thing to just flat-out say given that people answer questions in multiple forums.
    Last edited by gimbal2; 03-16-2015 at 11:24 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    25

    Default Re: Array Test questions

    Some people try to garner sympathy as they think that encourages others to prove them wroong by doing their homework for them.

    That's why I called the bluff on the other site.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  13. #13
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Array Test questions

    Indeed, the one time that Jos actually is as nice as his regards make you believe he is, you get this insulting crud.

    EDIT: thankfully, your call is met satisfactory.

    Sorry if there was a misunderstanding, everyone here was very helpful and pleasant, there are other places that are not as friendly.
    Last edited by gimbal2; 03-17-2015 at 09:29 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  14. #14
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Array Test questions

    Quote Originally Posted by Doghouse View Post
    I thought the execution stops when an exception is thrown.
    Java Code:
    public class Exceptions {
    
       public static void main(String[] args) throws Exception {
         for (int j = 1; j < 6; j++) {
            try {
               method(j);
             }
             catch (IllegalArgumentException iae) {
                System.out.print(iae.getMessage());
             }
          }
       }
    
       public static void method(int v) {
          switch (v) {
             case 1:
                throw new IllegalArgumentException("Then");
             case 2:
                throw new IllegalArgumentException(" you");
             case 3:
                throw new IllegalArgumentException(" didn't");
             case 4:
                throw new IllegalArgumentException(" try");
             case 5:
                throw new IllegalArgumentException(" this.\n");
          }
       }
    }
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Questions about my code and JUnit Test
    By TaniaG. in forum New To Java
    Replies: 9
    Last Post: 11-21-2014, 03:58 PM
  2. Help me with the test review questions please
    By ssunlimited in forum New To Java
    Replies: 9
    Last Post: 10-18-2013, 08:17 PM
  3. Please Help Me!!! Last minute test questions...
    By justinm231 in forum New To Java
    Replies: 1
    Last Post: 11-10-2011, 06:42 PM
  4. 2 test questions on JSP.
    By an24 in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 12-22-2009, 11:14 AM
  5. Some Questions - from old test papers 100 level
    By Julie in forum New To Java
    Replies: 5
    Last Post: 09-03-2008, 06:20 AM

Posting Permissions

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