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 9 of 9

Thread: Help so stumped....

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Help so stumped....

    i have this program.. basically it keeps reading an integer until the user inputs 0... then it outputs the count of positives and negatives, the sum of the postives and the negatives, largest values and lowest value..

    basically everything is working all except the maximum negative value...
    it always prints out 0 as an output.... could any1 help me? im a newbie so sorry for the shitty code.

    import java.util.Scanner;
    import java.io.*;
    public class lab31 {
     
     
     
    public static void Numbers (String [] args) {
             Scanner input=new Scanner (System.in);
     
    int sumnegatives = 0;
    int sumpositives = 0;
    int negatives = 0;
    int positives = 0;
    int number;
    int max = 0;
    int min = 0;
    int max2 = 0;
    int min2 = 0;
     
     
    System.out.println("Arithmetic Fun");
    System.out.println("Please enter series of numbers(to terminate enter 0)");
    System.out.print("Enter an integer: ");
    number=input.nextInt();
    if(number>0){
    max=number;
    min=number;
    }
    if(number<0){
    max2=number;
    min2=number;
    }
     
    while (number != 0){
     
    if (number > 0) {
                    positives++;
                    sumpositives += number;
     
                     if (number>max)
                         max=number;
                            if(number<min)
                                min=number;
                             }
     
     else if (number < 0){
         negatives++;
         sumnegatives += number;
     
                    if(number>max2)
                         max2=number;
                           if (number<min2)
                              min2=number;
                            }
     
     
     
     
    System.out.print("Enter an integer: ");
                    number=input.nextInt();
     
    }//while loop
     
    System.out.println("\nStatistics for positive values");
    System.out.println("\nCount of positive numbers  :  " + positives);
    System.out.println("Sum of positive numbers  :  " + sumpositives);
    System.out.println("Maximum positive numbers  :  " + max);
    System.out.println("Minumum positive numbers  :  " + min);
    System.out.println("\nStatistics for negative values");
    System.out.println("\nCount of negative numbers:   " + negatives);
    System.out.println("Sum of negative numbers  :  " + sumnegatives);
    System.out.println("Maximum negative number  :  " + max2);
    System.out.println("Minumum negative number  :  " + min2);
     
     
        }
    }
    Last edited by JavaPF; October 28th, 2010 at 04:58 AM. Reason: Please use [highlight=Java] code [/highlight] tags


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 286 Times in 225 Posts
    Blog Entries
    4

    Default Re: Help so stumped....

    I have compiled this code.

    It doesn't seem like the logic is quite right. Depending on the sequence you enter these numbers, the results differ.

    As an example, if you enter just negative numbers, the 'Maximum negative number' works fine.
    If you enter a negative number, then a positive number, the 'Minumum positive numbers' doesn't work.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

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

    Default Re: Help so stumped....

    The code initializes the max and min to zero, which will restrict their settings. Typically the better way to initialize these values is to a) initialize them to the first value or b) initialize to Integer.MAX_VALUE (min) or Integer.MIN_VALUE (max)

  4. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help so stumped....

    Quote Originally Posted by JavaPF View Post
    I have compiled this code.

    It doesn't seem like the logic is quite right. Depending on the sequence you enter these numbers, the results differ.

    As an example, if you enter just negative numbers, the 'Maximum negative number' works fine.
    If you enter a negative number, then a positive number, the 'Minumum positive numbers' doesn't work.
    so can you tell me whats wrong? i cant seem to determine... thanks i didnt realize that problem now im even more confused....

  5. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help so stumped....

    Quote Originally Posted by copeg View Post
    The code initializes the max and min to zero, which will restrict their settings. Typically the better way to initialize these values is to a) initialize them to the first value or b) initialize to Integer.MAX_VALUE (min) or Integer.MIN_VALUE (max)

    their initialized to 0 because if the user enters the first number.. it checks the number if its <0 or >0 then it enters the loop...

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

    Default Re: Help so stumped....

    Quote Originally Posted by Macgrubber View Post
    their initialized to 0 because if the user enters the first number.. it checks the number if its <0 or >0 then it enters the loop...
    Yes, but think about the logic: if the first number is positive, max2 is set to 0, which will always be great than any negative number entered (same goes for the alternative where the user enters a negative number first). Reread my post above carefully, and just as a test, try changing the initialization to
    int max2 = Integer.MIN_VALUE;
    and see what happens
    Last edited by copeg; October 28th, 2010 at 11:55 AM.

  7. #7
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help so stumped....

    it works if i the 1st number i enter is positive but if you enter a negative number 1st then it has problems

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

    Default Re: Help so stumped....

    Quote Originally Posted by Macgrubber View Post
    it works if i the 1st number i enter is positive but if you enter a negative number 1st then it has problems
    One more time, read my posts above. Then think about the logical flow of the program - manually go through the flow of the program with some input, writing down how the values change, you will see that it is necessary to initialize a max to a min, and a min to a max.

  9. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    13
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Help so stumped....

    o yeah thanks that was some big of a help....

Similar Threads

  1. Stumped?
    By KevinGreen in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 3rd, 2009, 01:02 AM