# Thread: [SOLVED] Array of first negative number

1. Member Join Date
Oct 2008
Location
Washington, US
Posts
28
Rep Power
0

## [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));

}
}```  Reply With Quote

2. ## 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;```  Reply With Quote

3. ## 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)  Reply With Quote

4. ##  Originally Posted by xcallmejudasx 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.  Reply With Quote

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

## 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'  Reply With Quote

6. ##  Originally Posted by random0munky 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  Reply With Quote

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

## 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.  Reply With Quote

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

##  Originally Posted by random0munky 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.  Reply With Quote

9. ## 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.  Reply With Quote

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

## 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  Reply With Quote

#### Posting Permissions

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