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


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Have trouble dealing with negative numbers.

  1. #1
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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;
    BufferedReader bR = new BufferedReader(new InputStreamReader(System.in));
    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);

    }

    }


  2. #2
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default 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.
    Last edited by elamre; July 27th, 2012 at 01:37 PM.

  3. #3
    Super Moderator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,101
    Thanks
    169
    Thanked 779 Times in 725 Posts
    Blog Entries
    5

    Default Re: Have trouble dealing with negative numbers.

    elamre
    Please read the forum rules, and please read the following:
    The Problem with Spoon-feeding

  4. #4
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default 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. #5
    Junior Member
    Join Date
    Jul 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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. #6
    Member
    Join Date
    Jun 2012
    Location
    Left Coast, USA
    Posts
    451
    My Mood
    Mellow
    Thanks
    1
    Thanked 97 Times in 88 Posts

    Default Re: Have trouble dealing with negative numbers.

    Quote Originally Posted by Arick Cheng View Post
    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
    Last edited by Zaphod_b; July 29th, 2012 at 08:04 AM.

  7. #7
    Member
    Join Date
    Jul 2012
    Posts
    69
    My Mood
    Relaxed
    Thanks
    1
    Thanked 6 Times in 6 Posts

    Default 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)

Similar Threads

  1. Replies: 17
    Last Post: July 27th, 2012, 12:52 AM
  2. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  3. Method returning negative numbers seemingly at random
    By NcAdams in forum What's Wrong With My Code?
    Replies: 3
    Last Post: May 24th, 2012, 09:53 AM
  4. Help with code dealing with parallel arrays.
    By danielp1213 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 13th, 2011, 06:43 PM
  5. My program keeps lagging when dealing with large arrays.
    By chrynelson in forum Java Theory & Questions
    Replies: 4
    Last Post: October 21st, 2011, 04:57 PM