Results 1 to 10 of 10
- 12-08-2008, 10:32 PM #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)); } }
-
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;
- 12-08-2008, 10:35 PM #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)
-
- 12-08-2008, 10:40 PM #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'
- 12-08-2008, 10:47 PM #6
- 12-08-2008, 10:53 PM #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.
- 12-08-2008, 11:10 PM #8
Member
- Join Date
- Dec 2008
- Location
- Enschede, Overijssel, the Netherlands
- Posts
- 19
- Rep Power
- 0
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.
- 12-08-2008, 11:13 PM #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.
- 12-08-2008, 11:17 PM #10
Member
- Join Date
- Oct 2008
- Location
- Washington, US
- Posts
- 28
- Rep Power
- 0
Similar Threads
-
Finding the largest number in an array
By starchildren3317 in forum New To JavaReplies: 14Last Post: 11-03-2010, 06:49 AM -
how to extract the number of the image which looks like a number
By Crest.Boy in forum Java ServletReplies: 1Last Post: 11-03-2008, 02:38 PM -
Counting the number of columns in a 2D array,
By KalEl in forum New To JavaReplies: 9Last Post: 10-21-2008, 05:27 AM -
initialize a number, which is read in from a file, into an array
By little_polarbear in forum New To JavaReplies: 19Last Post: 06-10-2008, 03:53 AM -
how to right a program that find kth number in two sorted array?
By fireball2008 in forum New To JavaReplies: 8Last Post: 04-22-2008, 03:21 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks