# Removing negative number from Array

Page 1 of 1

## 5 Replies - 1838 Views - Last Post: 21 July 2014 - 06:38 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=350736&amp;s=44fde6665ba1d022dd53e6e952bafdeb&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 misheekoh

Reputation: 0
• Posts: 3
• Joined: 20-July 14

# Removing negative number from Array

Posted 20 July 2014 - 05:42 PM

Hello, I am currently working on this assignment.

Ask the user to enter a sequence of at most 20 nonnegative integers. Your program should have a loop that reads the integers into an array and stops when a negative is entered (the negative number should not be stored). Invoke the average method to find the average of the integers in the array (send the array as the parameter).

My question is, how can I remove the negative number from the array and calculate the average of the posive elements without the negative ones? This is my code so far...

```import java.util.Scanner;
import javax.swing.JApplet;

public class Parameters
{
//-------------------------------------
//Calls the average and minimum methods
//with different numbers of parameters
//-------------------------------------
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
double mean1, mean2, mean3, mean4;
int count =0;
int[] list20 = new int[20];
int i = 0;
mean1 = average(42,69,37);
mean2 = average(35,43,93,23,40,21,75);
mean3 = average(13);
mean4 = average();

System.out.println("mean1 = " + mean1);
System.out.println("mean2 = " + mean2);
System.out.println("mean3 = " +mean3);
System.out.println("mean3 = " +mean4);

System.out.println("\nEnter up to 20 non-negative numbers:");

for(i=0; i<list20.length; i++)
{
System.out.print("Number " + (i+1) + ": ");
list20[i]=scan.nextInt();
list20[count++] =list20[i];

if(list20[i] < 0)
break;

}

```

Is This A Good Question/Topic? 0

## Replies To: Removing negative number from Array

### #2 Martyr2

• Programming Theoretician

Reputation: 4960
• Posts: 13,382
• Joined: 18-April 07

## Re: Removing negative number from Array

Posted 20 July 2014 - 06:02 PM

Scan the value into a variable first, then evaluate it before placing it into the array. The directions say, do not store the value into the array. Meaning you should not be doing the scan.nextInt() directly into the array. Put it into a variable, if variable is < 0, then you know you are at the end and simply don't put the value into the array.

### #3 misheekoh

Reputation: 0
• Posts: 3
• Joined: 20-July 14

## Re: Removing negative number from Array

Posted 20 July 2014 - 07:10 PM

Martyr2, on 20 July 2014 - 06:02 PM, said:

Scan the value into a variable first, then evaluate it before placing it into the array. The directions say, do not store the value into the array. Meaning you should not be doing the scan.nextInt() directly into the array. Put it into a variable, if variable is < 0, then you know you are at the end and simply don't put the value into the array.

/>

This might be embarassing... but how do I add the variable into the array?

```System.out.println("\nEnter up to 20 non-negative numbers:");
for(i=0; i <=list20.length; i++)
{
System.out.print("Number "+ (i+1) + ": ");
input = scan.nextInt();

if(input > 0)
{
input = list20[i];           //This is wrong..
}
else
break;

```

### #4 astonecipher

Reputation: 2143
• Posts: 8,548
• Joined: 03-December 12

## Re: Removing negative number from Array

Posted 20 July 2014 - 07:31 PM

```list20[i] = input;
```

For starters. Now, what is the length of list20 at the start of the loop? Generally, a while loop is used for a sentinel loop. You loop while a condition is true, (not a negative number).

### #5 misheekoh

Reputation: 0
• Posts: 3
• Joined: 20-July 14

## Re: Removing negative number from Array

Posted 20 July 2014 - 07:49 PM

astonecipher, on 20 July 2014 - 07:31 PM, said:

```list20[i] = input;
```

For starters. Now, what is the length of list20 at the start of the loop? Generally, a while loop is used for a sentinel loop. You loop while a condition is true, (not a negative number).

OMG..I've been doing it the wrong way round all this time?! ...
Well.. to answer your question.. I defined list20 to have 20 elements. But then the elements in the array starts at 0.

Thank you again.

### #6 g00se

• D.I.C Lover

Reputation: 3280
• Posts: 14,665
• Joined: 20-September 08

## Re: Removing negative number from Array

Posted 21 July 2014 - 06:38 AM

... and your while loop truth conditions must be

a. there are more numbers to enter (You can ask the user to enter 'q' to quit)
b. the number of valid numbers already entered does not exceed the number of available array elements