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: Second largest

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Second largest

    Hi.

    I'm trying to find the largest and the second largest number in vector. But I can't.

    Can you help?

    public class Minmax{
      public static void main(String[] args) {
     
        //int min = Integer.MIN_VALUE;
        int max = Integer.MAX_VALUE;
        int max2 =Integer.MAX_VALUE;
     
     
        while (!StdIn.isEmpty()) {
          int N = StdIn.readInt();
          if (N > max){
            max2=max;//storing the old max
    	max = N;//the new max
    		}
     
          else if (N>max2 && N<max) {//if the number is between new max and the old max
            max2=N;
          }
    	}
    	StdOut.println(max2 + " " + max);
      }
    }
    Error message:
    C:\>java Minmax 2 22 5 55 6 66
    ^Z
    2147483647 2147483647

    C:\Users>java Minmax
    55 2 66 2 88 5 99
    ^Z
    2147483647 2147483647


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Second largest

    Remind me, what is StdIn? I think I remember it from a book I read a long time ago, but I've forgotten.

    It looks like you meant to use the command line input that is saved in the args[] array rather than StdIn. Is that right?

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Second largest

    StdIn is used in my textbook, Introduction to programming in java. It's a library thing for all kinds of inputs.

    Your right. But I think I can use it like that and press ctrl+enter

    --- Update ---

    This is the ''older'' version. As you can see it prints out the highest value twice.
    I'm trying to fix that.
    public class Minmax {
      public static void main(String[] args) {
     
        int min = Integer.MAX_VALUE;
        int max = Integer.MIN_VALUE;
        int max2 =Integer.MIN_VALUE;
     
        while (!StdIn.isEmpty()) {
          int N = StdIn.readInt();
          if (N > max) {
            max = N;
    	max2=max;
          }
          if (N < max && N>max2) {
            max2 = N;
          }
        }
        StdOut.println(max2 + " " + max);
      }
    }

    C:\java Minmax
    5
    55
    66
    55
    556
    5
    557
    ^Z
    557 557

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Second largest

    It's interesting that you changed the beginning values to MAX from MIN. If you want to find a large number from a collection, you start by comparing it to a smaller number, not a number that it couldn't possibly be larger than.

    I also don't see the purpose of your else statement. Try changing the initial values back to MIN, eliminate the else clause, and I think you'll be there.

  5. #5
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Second largest

    It did not work out for me.

    How about this. Find the three largest numbers and print them out.

    public class Minmax2 {
      public static void main(String[] args) {
     
        int max = Integer.MIN_VALUE;
        int max1= Integer.MIN_VALUE;
        int max2 =Integer.MIN_VALUE;
     
        while (!StdIn.isEmpty()) {
          int N = StdIn.readInt();
          if (max<N) {
            max2 = max1;
    		max1=max;
    		max=N;
    		}
    		if (max1<N) {
    			max2= max1;
    			max1 =N;
    			}
    			else if(max2<N){
    				max2=N;
    				}
        }
      StdOut.println(max2 + " " + max1 + " " + max);
    }}

    C:\>java Minmax2
    55
    66
    88
    -1
    66
    67
    68
    ^Z
    68 88 88

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Second largest

    You're right, I over simplified, but why did you change the problem?

    Here's my for loop using an array that contains the same numbers you used in the same order for the 2 largest numbers, not 3:
    int[] numbers = { 55, 66, 88, -1, 66, 67, 68 };
     
    for ( int i = 0 ; i < numbers.length ; i++ )
    {
        int N = numbers[i];
        if (N > max)
        {
            max2=max;//storing the old max
            max = N;//the new max
        }
     
        // find values between current max and max2 for new max2
        if ( N < max && N > max2 )
        {
            max2 = N;
        }
    }

  7. The Following User Says Thank You to GregBrannon For This Useful Post:

    JohnJohnson123 (October 18th, 2013)

  8. #7
    Junior Member
    Join Date
    Sep 2013
    Posts
    22
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Default Re: Second largest

    I thought it was more fun like this. I saw it on stackverflow.

    Thanks for the ongoing support.

Similar Threads

  1. largest object in an array
    By melek in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 15th, 2013, 10:43 PM
  2. find largest value in BST
    By Java Bean in forum What's Wrong With My Code?
    Replies: 41
    Last Post: December 10th, 2012, 03:42 PM
  3. Find the two largest number
    By vendettabf in forum What's Wrong With My Code?
    Replies: 15
    Last Post: December 29th, 2011, 01:23 PM
  4. [SOLVED] My smallest and largest integers will not change.
    By toiletsauce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 1st, 2011, 07:50 PM
  5. finding the largest object help
    By nickypass in forum Object Oriented Programming
    Replies: 4
    Last Post: October 16th, 2010, 05:48 PM