Negative interger.

 DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

1. Registered User
Join Date
Oct 2004
Posts
12

## Negative interger.

This is the actual problem that I am solving.
________________________________________________________________

Write a program that takes 10 integers as input. The program places the even integers into an array called evenLIst, the odd integers into an array called oddList, and the negative integers into an array called negativeList. The program displays the contents of the three arrays after all of the integers have been entered.

_________________________________________________________________

After I insert a negative integer, it places it under negative only. For an example, if I input -10, it only places it in the negativeList. But I want it to also put it in the evenList, hence it's negative and positive.

Here is my source code -----
<code>
import java.util.Scanner;
public class project8_1 {
public static Scanner scan = new Scanner(System.in);
public static void main(String[] args) {
int even=0, odd=0, negative=0;
int evenList[] = new int[9];
int oddList[] = new int[9];
int negativeList[] = new int[9];
int count=0;
boolean valid = true;
for(count=0; count<10; count=count+1){
System.out.print("Enter an integer:");
valid = scan.hasNextInt();
while (valid==false){
scan.nextLine();
valid=scan.hasNextInt();
}
int x = scan.nextInt();
if (x<0){negativeList[negative]=x; negative=negative+1;}
if (x>=0 && x%2==0){evenList[even]=x; even=even+1;}
if (x>=0 && x%2==1){oddList[odd]=x; odd=odd+1;}
}
count=0;
System.out.println("Negative Numbers:");
while(count<negative){System.out.println(negativeList[count]+" "); count=count+1;}
count=0;
System.out.println("Even Numbers:");
while(count<even){System.out.println(evenList[count]+" "); count=count+1;}
count=0;
System.out.println("Odd Numbers:");
while(count<odd){System.out.println(oddList[count]+" "); count=count+1;}

}
}
</code>

2. ## Modulus operator is not an implicit abs...

Like:

int x=-7;
int y=x%2; // <--- y is now -1

you should do like:
Code:
```if (x < 0) negativeList[negative++] = x;
if (x % 2 == 0) evenList[even++] = x;
if (Math.abs(x % 2) == 1) oddList[odd++] = x;```
PS: the code tag is w. brackets ('[' and ']'), not the '<' and '>'

3. Registered User
Join Date
Jan 2005
Posts
38

## Solution

This my Code you can use it or take the idea from it>>>
Code:
```import java.util.*;
import java.io.*;
public class Sep
{
public static void main(String[]args) throws IOException
{
ArrayList all = new ArrayList();
ArrayList evenList = new ArrayList();
ArrayList oddList = new ArrayList();
ArrayList negList = new ArrayList();
for(int i=0;i<10;i++)
{
System.out.print("Enter an integer: ");
}

for (int i=0;i<all.size();i++)
{
Integer num = (Integer)all.get(i);
int number = num.intValue();
if (number<0&&number%2==0)
{
}

else if (number<0&&number%2!=0)
{
}

else if (number>0&&number%2==0)
{
}

else if (number>0&&number%2!=0)
{
}
}

System.out.println("The Negative List: "+negList);
System.out.println("The Odd List: "+oddList);
System.out.println("The Even List: "+evenList);
}
}```

Try it then Tell me the Result>>>
txt file is in attachment...

#### Posting Permissions

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

 FAQ Latest Articles Java .NET XML Database Enterprise