# Have trouble dealing with negative numbers.

• July 27th, 2012, 06:20 AM
Arick Cheng
Have trouble dealing with negative numbers.
I am doing a primesum program. The aim of this program is to find the sum of prime integers. I have a problem here dealing with negative numbers. What should I add inside my code to ignore negative inputs? Anyone can help me?

Here is my code below. It is doing something but it does not ignore my negative inputs.

public class primesum
{
public static int checkPrime(int entry)
{
int x = entry; //Initial sum of the primes
int count = 1; //The modulus factor
while(count <= Math.sqrt(entry))
{
if(entry % count != 0)
{
count++; //Do the next modulus factor
}
else
{
x = 0;
break;
}
}

return x; //Adds all the sum up and returns the total sum
}
public static void main(String[] args)
{
int x = 0;
System.out.println("Please enter some 32-bit integers: ");
int entry = 1;
try{
while(entry != 0){
String line = bR.readLine();
entry = Integer.parseInt(line);
x += checkPrime(entry); //Checks whether x is a prime
}
}
catch(Exception ex){
System.out.println(ex);
}
System.out.println("The sum of the primes is " + x);

}

}
• July 27th, 2012, 06:56 AM
elamre
Re: Have trouble dealing with negative numbers.
Try to use the code tags, it makes it so much easier to read and understand your code :)
So all you want to do is handle negative numbers? Ignore them? or make them positive?

What you could do is check if the number is negative for example with a statement. And then react on it.

Also try to start your class with a capital letter, this is a common way of marking everything. just like how variables start with a smart letter.
• July 27th, 2012, 12:36 PM
copeg
Re: Have trouble dealing with negative numbers.
elamre
http://www.javaprogrammingforums.com...n-feeding.html
• July 27th, 2012, 01:40 PM
elamre
Re: Have trouble dealing with negative numbers.
Ok sorry, just trying to help here.
I edited the post completely. That topic should be better accesible. I did not know it existed.
I normally like to have the answer and then work backwards to figure out myself how it works. More fun for me :). But i did not know this forum rather goes the teaching way. My bad.
• July 29th, 2012, 01:35 AM
Arick Cheng
Re: Have trouble dealing with negative numbers.
I am trying to make the program ignore negative inputs to it. (So, when the user enters a negative number before a zero termination, the final output for the total sum of primes should not have negative numbers added to them.) This means the program only terminates when the user types in 0 with an enter key.
• July 29th, 2012, 07:40 AM
Zaphod_b
Re: Have trouble dealing with negative numbers.
Quote:

Originally Posted by Arick Cheng
I am trying to make the program ignore negative inputs to it....

With the way your program is organized you can do something like this:

Code :

```SET THE VALUE OF entry TO 1 WHILE entry IS NOT EQUAL TO ZERO BEGIN LOOP GET a value of entry from the user. IF entry IS GREATER THAN TO ZERO THEN Do Something. END IF END LOOP```

A more user-friendly program with the same overall organization might look like this:

Code :

```SET THE VALUE OF entry TO 1 WHILE entry IS NOT EQUAL TO ZERO BEGIN LOOP GET a value of entry from the user. IF entry IS GREATER THAN ZERO THEN Do Something. ELSE IF entry IS LESS THAN ZERO THEN TELL the user that negative values will be ignored. END IF END IF END LOOP Tell the user goodbye.```

Cheers!

Z
• July 29th, 2012, 07:41 AM
elamre
Re: Have trouble dealing with negative numbers.
Well, you can use an
Code java:

`if(statement)`
to check if the number is smaller then 0, and thus being negative. Then you could ignore negative numbers. And you could also check if the number is equal to 0.
Code java:

`if(number==0)`