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

Thread: Printing the Max and Min in an Array

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

    Default Printing the Max and Min in an Array

    Another one for you guys if you want to help me out... This program is supposed to print the maximum and minimum value in an array. It doesn't. There's a problem in my logic and I cannot figure it out! I've been staring at it too long.

    I'm new to Java, that's why all these are pretty simple to you gurus... thoughts?

    EDIT: I lied, I wanted first and second maximum numbers. Sorry :$

     class A3Q3b
    {
        public static void main (String[] args) 
        {  
         // DECLARE VARIABLES/DATA DICTIONARY 
         int [] a;           // GIVENS: the array of values
         int index;          // INTERMEDIATES: index through array
         int first, second;  // RESULTS: values of Max and Second Max
     
         System.out.println( "Please input the array:  " );
         a =  ITI1120.readIntLine();
     
          // BODY OF ALGORITHM
         first =  a[0]; second = a[0];
         index = 1; 
     
         while (index > a.length)
         { 
           if (a[index] < first)
           { 
              second = first;
              first = a[index];
           }
           else
           { 
             if (a[index] < second) 
             {
                second = a[index];
             }
     
           }
           index = index + 1;  
         }
     
         System.out.println("The maximum number is " + first);
         System.out.println("The second number is " + second);
      }
    }
    Last edited by bonbon242; October 29th, 2010 at 08:14 AM.


  2. #2
    Member
    Join Date
    Oct 2010
    Location
    Denver, CO
    Posts
    55
    Thanks
    1
    Thanked 30 Times in 29 Posts

    Default Re: Printing the Max and Min in an Array

    Are your <'s and >'s backwards? And probably easier to use a for loop to count through an array

    int min = a[0];
    int max = a[0];

    for(int i = 0;i<a.length;i++) {
    if(a[i]>max) max = a[i];
    if(a[i]<min) min = a[i];
    }

    if you wanted first and second highest, as it seems in the code, I would
    int min = a[0];
    int max = a[0];
    int second = a[0];

    for(int i = 0;i<a.length;i++) {
    if(a[i]>max){
    second = max;
    max = a[i];
    }
    if(a[i]<min) min = a[i];
    }

  3. The Following User Says Thank You to Zula For This Useful Post:

    bonbon242 (October 29th, 2010)

  4. #3
    Junior Member
    Join Date
    Oct 2010
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Printing the Max and Min in an Array

    That did it. Thanks a lot

Similar Threads

  1. Doubt Regarding Printing an Array List
    By reeceypp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2010, 03:11 AM
  2. [SOLVED] Printing Array without printing empty elements
    By CarlMartin10 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 12th, 2010, 02:41 AM
  3. Printing a JTable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 08:15 AM
  4. How to printing a Jtable
    By hundu in forum AWT / Java Swing
    Replies: 0
    Last Post: June 29th, 2009, 06:57 AM
  5. Printing JTable that retrieve data from the Database
    By hundu in forum AWT / Java Swing
    Replies: 3
    Last Post: June 28th, 2009, 01:50 PM