# Method Doesnt turn negative number 0 and doesnt return 3

Printable View

• 09-27-2010, 09:56 PM
anonb
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[5];
Scanner input = new Scanner(System.in);

System.out.println("Please enter numbers: ");
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];
}
}
}
}
• 09-27-2010, 10:15 PM
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.
• 09-27-2010, 10:40 PM
anonb
Its supposed to take negative integers, shift them to right, and add them as 0's.

Output is:

Please enter numbers:
3 5 -2 4 -3
Returned size of aray is 5
3
5
4
-3
-3
• 09-27-2010, 10:48 PM
anonb
import java.io.*;
import java.util.*;

public class untitled2
{

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

System.out.println("Please enter numbers: ");
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];
}
}
}
}
• 09-27-2010, 10:56 PM
Norm
Quote:

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

Quote:

Please enter numbers:
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???
• 09-27-2010, 11:35 PM
Zack
Quote:

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.
• 09-27-2010, 11:57 PM
anonb
yea but how do I do that?
• 09-28-2010, 12:17 AM
Zack
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.