# Thread: Method Doesnt turn negative number 0 and doesnt return 3

1. Member Join Date
Sep 2010
Posts
13
Rep Power
0

## Method Doesnt turn negative number 0 and doesnt return 3

Write a method that accepts an integer array of unspecified length as a parameter. It should remove all negative values from the array and return the size of the altered array. Unused values at the end of the array should be filled with zeroes. For example, the array {3, 5, -2, 4, -3} should become {3, 5, 4, 0, 0} and 3 should be returned as the new length. Do not copy the array, modify the array that was passed as a parameter. What is the Big-O of this method?

import java.io.*;
import java.util.*;

public class untitled2
{

public static void main(String args[])
{
//creating
int temp = 0;
int [] number = new int;
Scanner input = new Scanner(System.in);

int j;
//first loop to
for (int i=0; i<number.length;i++)
{
number[i] = input.nextInt();
//System.out.print(number[i]);
}
System.out.println("Returned size of aray is " + shift(number));
for (int i=0; i<number.length;i++)
{
System.out.println(number[i]);

}
}
public static int shift(int []number)
{
int j=0;
int temp=0;
int k=0;
for (int i=0; i<number.length;i++)
{

if (number[i] < 0)
{
for (j=i; j < number.length-1;j++)
number[j]=number[j+1];
}
}
}
}  Reply With Quote

2. Senior Member Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0

## I have a few problems with what you've posted:
1) No [code] tags around the code.
2) There doesn't appear to be a specific question attached. What exactly is the problem with what you have? What does it do/doesn't it do that it shouldn't/should? What is the output of what you have?
3) You don't have any return statement in your shift() method.  Reply With Quote

3. Member Join Date
Sep 2010
Posts
13
Rep Power
0

## Its supposed to take negative integers, shift them to right, and add them as 0's.

Output is:

3 5 -2 4 -3
Returned size of aray is 5
3
5
4
-3
-3  Reply With Quote

4. Member Join Date
Sep 2010
Posts
13
Rep Power
0

## import java.io.*;
import java.util.*;

public class untitled2
{

public static void main(String args[])
{
//inializing array and scanner
int [] number = new int;
Scanner input = new Scanner(System.in);

int j;

for (int i=0; i<number.length;i++)
{
number[i] = input.nextInt();
}
//calls second method
System.out.println("Returned size of aray is " + shift(number));
for (int i=0; i<number.length;i++)
{
System.out.println(number[i]);
}
}
//this method shifts negative numbers to right
public static int shift(int []number)
{
int j=0;
for (int i=0; i<number.length;i++)
{

if (number[i] < 0)
{
for (j=i; j < number.length-1;j++)
number[j]=number[j+1];
}
}
}
}  Reply With Quote

5. ## supposed to take negative integers, shift them to right, and add them as 0's
can you explain what that means?

3 5 -2 4 -3
Returned size of aray is 5
3
5
4
-3
-3
What does the column of numbers represent? The input had a -2 that is not in the column. The order of the numbers in the column is different from the order in the row above it???  Reply With Quote

6. Senior Member Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0

##  Originally Posted by Zack I have a few problems with what you've posted:
1) No [code] tags around the code.
2) There doesn't appear to be a specific question attached. What exactly is the problem with what you have? What does it do/doesn't it do that it shouldn't/should? What is the output of what you have?
3) You don't have any return statement in your shift() method.
You still haven't dealt with #s 1 and 3...

As for 2, all it does is shift the numbers backward, so you'd go from {1, -2, 3, 4} to {1, 3, 4, 4}. If you want to replace them with 0s, you have to set the last element of the array to 0 each time you shift it over one.  Reply With Quote

7. Member Join Date
Sep 2010
Posts
13
Rep Power
0

## yea but how do I do that?  Reply With Quote

8. Senior Member Join Date
Jun 2010
Location
Destiny Islands
Posts
690
Rep Power
0

## You already know how to change an element based on its index in the array. You also know how to get the length of the array. Putting those two concepts together, you can edit the last element in the array.  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
•