Welcome to the Java Programming Forums

The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

>> REGISTER NOW TO START POSTING

# Thread: Have trouble dealing with negative numbers.

1. ## 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){
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);

}

}

2. ## 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.

3. ## Re: Have trouble dealing with negative numbers.

elamre
The Problem with Spoon-feeding

4. ## 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.

5. ## 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.

6. ## Re: Have trouble dealing with negative numbers.

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:

```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:

```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

7. ## Re: Have trouble dealing with negative numbers.

Well, you can use an
`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.
`if(number==0)`