Results 1 to 7 of 7
  1. #1
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default Arrays and use of null or empty inputs

    Hi there

    I have an assignment that I managed to to to a large extent. My problem is on how to code for a situation when the inPut numbers is null or empty. Please check the problem in it's entirety below:


    This method takes an array of numbers as input and returns the index of the largest number in that array. If the largest number appears multiple times, ties are settled by returning the index of its first appearance. For instance, given [4, 2, 7, 1, 7, 3] as input, the method returns 2 (the index of the first appearance of 7 in the array).

    If inputNumbers is null or empty, the method should return -1 to indicate an error condition.



    This is the part that has given me sleepless nights "If anArray input is null or empty, the method should return -1 to indicate an error condition. "


    Here is the code that I used"

    XML Code:
    class assign1 {
         public static void main(String[] args) {
              int[] anArray = { 4, 2, 7, 1, 7, 3}; //used this for testing
    
    int startIndex =0;
    int max = anArray[startIndex];
    int indexOfMax = startIndex;
    
    for (int index = startIndex + 1; index< anArray.length; index++)
    {
     if (anArray[index]>max)
       { 
        max = anArray[index];
        indexOfMax = index;
    }
    }
    System.out.println(indexOfMax);
    
    }
    }

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,430
    Rep Power
    7

    Default

    I'd write a validateInput() method that's called after inputting/filling the array, that loops over the array and sets all invalid numbers to -1.
    BTW: in an int array you cannot have null or empty fields, they're all initialzed with 0 as the default value.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    The simplest solution is to use an Integer array. As PhHein already mentioned, int arrays are initialized with the default value 0, whereas Integers (objects, not primitives) are initialized as null.
    Ever seen a dog chase its tail? Now that's an infinite loop.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    Ah, actually it could be empty...I would consider an int[] of length 0 to be empty.

  5. #5
    al_Marshy_1981 is offline Senior Member
    Join Date
    Feb 2010
    Location
    Waterford, Ireland
    Posts
    748
    Rep Power
    5

    Default

    Quote Originally Posted by Desmond View Post
    Here is the code that I used"

    XML Code:
    class assign1 {
         public static void main(String[] args) {
              int[] anArray = { 4, 2, 7, 1, 7, 3}; //used this for testing
    
    int startIndex =0;
    int max = anArray[startIndex];
    int indexOfMax = startIndex;
    
    for ([FONT="Arial Black"]int index = startIndex + 1[/FONT]; index< anArray.length; index++)
    {
     if (anArray[index]>max)
       { 
        max = anArray[index];
        indexOfMax = index;
    }
    }
    System.out.println(indexOfMax);
    
    }
    }
    why are you skipping the arrays first element i.e. index[0].

    as for your original question, java will complain if an array is declared as Null and then tried to be accessed, you will have to use a try catch to output a -1

  6. #6
    Desmond is offline Member
    Join Date
    Mar 2010
    Posts
    17
    Rep Power
    0

    Default NullPointerException - String Arrays

    Hi guys below is a task question and my code with compiless and runs but cannot handle a possibility that the input array is null. May someone advise on where the bug is:-

    This method takes an array of strings and a probe string as input. It checks if the probe string appears in the array of strings. If it does appear, checkForProbe returns true otherwise it returns false. Checking for the probe is case insensitive, e.g., "the" matches "the" as well as "The" and "THE". The probe can also be null, and some of the elements of inputStrings may also be null. Note that the null probe matches the null elements of inputStrings. For instance, given input array ["abcd", "def", null, "ghi", "jklmn", "xyz"] and probe "Ghi", the method returns true.

    The method prototype is:
    boolean checkForProbe(String[ ] inputStrings, String probe)

    If inputStrings is null or if it is an empty array, the method should return false.


    XML Code:
    boolean checkForProbe(String[ ] inputStrings, String probe) {
            boolean checkForProbe = false; 
              
             
            for (int i = 0; i< inputStrings.length; i++) 
              if ((inputStrings.length==0)||(inputStrings[i]==null)) 
               { 
                 checkForProbe = false; 
               } 
               else 
               { 
                for ( i = 0; i< inputStrings.length; i++) 
                 if (probe.equalsIgnoreCase( inputStrings[i])) 
               { 
               checkForProbe = true; 
              } 
            } 
            return checkForProbe;
        }
    }
    boolean checkForProbe(String[ ] inputStrings, String probe) {
    boolean checkForProbe = false;


    for (int i = 0; i< inputStrings.length; i++)
    if ((inputStrings.length==0)||(inputStrings[i]==null))
    {
    checkForProbe = false;
    }
    else
    {
    for ( i = 0; i< inputStrings.length; i++)
    if (probe.equalsIgnoreCase( inputStrings[i]))
    {
    checkForProbe = true;
    }
    }
    return checkForProbe;
    }



    Example inputStrings:
    {"The", "Sun", "rises", "in", "the", "east"}
    Example probe string:
    null

    Your code caused an Exception:
    java.lang.NullPointerException


    Comments:
    Your program must handle the case where the probe is the null string.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,755
    Rep Power
    19

    Default

    For starters, you're not checking if inputStrings is null.
    And you should also check if probe is null...I mean, I'm not sure what else I can say, since the answer is in the question.

    A comment on your code though:
    Java Code:
    inputStrings.length==0
    is always going to be false inside that loop, so it's a pointless check.

    ETA: just to clarify, this is the line you'll get a null pointer from if probe is null:
    Java Code:
    probe.equalsIgnoreCase( inputStrings[i])
    So you need another check to see if probe is null.

Similar Threads

  1. Someone plz help... how to block inputs
    By waklo99 in forum New To Java
    Replies: 4
    Last Post: 03-15-2010, 06:44 AM
  2. empty arrays.
    By circuspeanuts in forum New To Java
    Replies: 11
    Last Post: 04-06-2009, 07:08 PM
  3. temperature program with inputs
    By JingGong in forum New To Java
    Replies: 3
    Last Post: 10-17-2008, 06:34 PM
  4. How to create this if many inputs?
    By sarahannel123 in forum New To Java
    Replies: 3
    Last Post: 05-18-2008, 04:22 PM
  5. Date Inputs
    By hiranya in forum AWT / Swing
    Replies: 3
    Last Post: 11-06-2007, 05:11 PM

Posting Permissions

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