Results 1 to 10 of 10
  1. #1
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default [SOLVED] Array of first negative number

    Hey everyone. I'm a little stumped with this. I want to scan the supplied array and then scan the array for the 'index of the first negative number' so if you have an array { '5','-9','-1'} the first index negative number would be the -9. Also if there aren't any negative numbers in the supplied array, it returns a -1 automatically. Here is what I have so far:

    Java Code:
    public class ArrayProblems {
    	private static String input ;
    	
    	private static boolean containsAllDigits(char[] charArray) {
    		int i ;
    		 for (i = 0; i < charArray.length ; i++) {
    			 if (!Character.isDigit(input.charAt(i)))
    	                return false;
    		 }
    		 return true ;
    	}
    	
    	private static int indexOfFirstNegativeNumber(int[] intArray) {
    		int i ;
    		int negative = 0 ;
    		for (i = 0 ; i < intArray.length ; i++){
    			if (negative < intArray[i])
    			{
    				negative = intArray[i] ;
    			}
    			
    		}
    		return -1 ;				
    	}
    
    	public static void main(String[] args) {
    		int i ;
    		//***********************************************************
    		// Testing containsAllDigits
    		System.out.println("Testing containsAllDigits\n");
    		char[] testCharArray = {'a', '3', '5', '0'};
    		input = new String(testCharArray) ;
    		System.out.print("Test array is: ");
    		for (i=0; i < testCharArray.length; i++) {
    			System.out.print(testCharArray[i] + " ");
    		}
    		System.out.println("\nContains all digits returns: "
    			+ containsAllDigits(testCharArray));
    		System.out.println("\n***************************");
    
    		//***********************************************************
    		// Testing indexOfFirstNegativeNumber
    	
    	System.out.println("Testing indexOfFirstNegativeNumber\n");
    	int[] testIntArray = {1, -13, -5, 0};
    	System.out.print("Test array is: ");
    	for (i=0; i < testIntArray.length; i++) {
    		System.out.print(testIntArray[i] + " ");
    	}
    	System.out.println("\nIndex of first negative number returns: "
    		+ indexOfFirstNegativeNumber(testIntArray));
    
    	}
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Not sure what you meant here. Return the index of the first neg?
    Java Code:
    for (i = 0 ; i < intArray.length ; i++)
    {
      if (intArray[i] < 0)
      {
        return i; //?
      }
    }
    return -1;

  3. #3
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Just a quick question. What if the first negative number is -1? How will you distinguish if it returned that because it was the first negative or because there are no negatives?

    You might want to set your sentinel to something like 999 or -999(some arbitrary number you know won't be getting used)

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by xcallmejudasx View Post
    Just a quick question. What if the first negative number is -1?
    from how I'm reading his question, he wants the index of the first negative, and an index will never be negative. His post isn't fully clear and is certainly open to multiple interpretations. It would be nice for the OP to come back and clarify all this up for us.

  5. #5
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Unfortuantely the assignment said to return a -1 if there weren't any negative numbers remember the array will be printed out to be shown to the user first and then java will find the first indexed negative number. To help clarify the first index negative number and what that really means I'll go expand. What it pretty much is asking is, reading from left to right through the array { '1', '-4', '-2' } The first indexed negative number is -4 reading from left to right. The first indexed number would be the '1'

  6. #6
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    Quote Originally Posted by random0munky View Post
    Unfortuantely the assignment said to return a -1 if there weren't any negative numbers remember the array will be printed out to be shown to the user first and then java will find the first indexed negative number. To help clarify the first index negative number and what that really means I'll go expand. What it pretty much is asking is, reading from left to right through the array { '1', '-4', '-2' } The first indexed negative number is -4 reading from left to right. The first indexed number would be the '1'
    Ahh that clears it up. Ignore my post then I thought you wanted to actually return the negative number. Carry on my wayward son

  7. #7
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    Ah Solved it well the teacher helped me haha here's what the question is asking:

    the first index of the negative number of for example this array { '5','-6','-7' } the first index would be 1. Because you first count 0 then 1 then 2 and so forth. Here's the code which works =D
    Java Code:
    	private static int indexOfFirstNegativeNumber(int[] intArray) {
    		int j = -1 ;
    		for (int i = 0 ; i < intArray.length ; i++){
    			if ((intArray[i] < 0) && (j == -1))
    			{
    				j = i ;	
    			}
    		}
    	
    		return j ;	
    	}

    Here's the output:

    Java Code:
    Testing containsAllDigits
    
    Test array is: a 3 5 0 
    Contains all digits returns: false
    
    ***************************
    Testing indexOfFirstNegativeNumber
    
    Test array is: 1 -13 5 0 
    Index of first negative number returns: 1
    Last edited by random0munky; 12-08-2008 at 11:20 PM.

  8. #8
    Sven is offline Member
    Join Date
    Dec 2008
    Location
    Enschede, Overijssel, the Netherlands
    Posts
    19
    Rep Power
    0

    Default

    Quote Originally Posted by random0munky View Post
    Lets see umm here's again what I have updated:
    Java Code:
    private static boolean containsAllDigits(char[] charArray) {
    		int i ;
    		 for (i = 0; i < charArray.length ; i++) {
    			 if (!Character.isDigit(input.charAt(i)))
    	                return false;
    		 }
    		 return true ;
    	}
    	
    	private static int indexOfFirstNegativeNumber(int[] intArray) {
    		int j = -1 ;
    		int i = 0 ;
    		for (i = 0 ; i < intArray.length ; i++){
    			if (intArray[i] < j && j==-1)
    			{
    				i = j ;
    				
    			}
    			
    		}
    		
    		return -1 ;				
    	}
    except it just goes into a endless loop though =\


    mmm just found that well I think it would be best to just compare each entry into the array and see if its less then zero if there aren't any entries in the array that's less than zero, I'll have it return the -1
    I still don't get if you want to return the *index* of the first negative number or the first negative number *itself*. Your posts are somewhat confusing. As far as I can see, your method is called indexOfFirstNegativeNumber and that is what you're trying to find; the index (position) of the first negative number in your array.

    Your program enters an infinite loop because you are assigning j to i in the loop, and what would be the value of j given the "if ( intArray[i] < j && j == -1 )" condition? This will always happen whenever it encounters a negative value in your array.

  9. #9
    xcallmejudasx's Avatar
    xcallmejudasx is offline Senior Member
    Join Date
    Oct 2008
    Location
    Houston, TX & Flint, MI
    Posts
    609
    Rep Power
    6

    Default

    It goes into an endless loop because your resetting the incrementing variable(i) to -1.

    You might have meant to put intArray[i] = j;
    or maybe j = i;
    or maybe j = intArray[i];


    I'm a little lost on the logic of this loop. Your saying if some number in your array is negative AND j == -1(which it will always because you never change j so you can just remove that part) then set i(your counter) to -1.

    Aren't you supposed to return i once the if statement is true and then end your loop since that will be the first instance where a negative occurs.

  10. #10
    random0munky is offline Member
    Join Date
    Oct 2008
    Location
    Washington, US
    Posts
    28
    Rep Power
    0

    Default

    I've solved the problem, just scroll up to my last post which is # 7 ;) thanks again for everyone's help with this great appreciate it =D

Similar Threads

  1. Finding the largest number in an array
    By starchildren3317 in forum New To Java
    Replies: 14
    Last Post: 11-03-2010, 06:49 AM
  2. Replies: 1
    Last Post: 11-03-2008, 02:38 PM
  3. Counting the number of columns in a 2D array,
    By KalEl in forum New To Java
    Replies: 9
    Last Post: 10-21-2008, 05:27 AM
  4. initialize a number, which is read in from a file, into an array
    By little_polarbear in forum New To Java
    Replies: 19
    Last Post: 06-10-2008, 03:53 AM
  5. Replies: 8
    Last Post: 04-22-2008, 03:21 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
  •