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

Thread: Coding error

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

    Default Coding error

    I want the program to print the max and 2nd max value of an array of integers

    thanks
    // This program prints the maximum and the second maximum elemnet in an array
     
    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 = 0;
         int second = 0;  // 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 = 0;
         for (int i = 0; i < a.length; i++)
         {
           if (a[i] > first)
           {
              second = first;
              first = a[i];
           }
         }
         for (int j = 0; j < a.length; j++)
           {
             if (first > a[j])
             {
                second = a[j];
             }
           }
           //index ++;
         System.out.println("The maximum number is " + first);
         System.out.println("The second number is " + second);
        }
    }
    Last edited by copeg; October 31st, 2010 at 10:49 AM.


  2. #2
    Junior Member
    Join Date
    Apr 2010
    Location
    Italy
    Posts
    15
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Default Re: Coding error

    Hi chemy G,
    a solution can be sort the array.
    The code is
    System.out.println( "Please input the array:  " );
    a =  ITI1120.readIntLine();
     
    Arrays.sort(a);
    first = a[a.length-1];
    second = a[a.length-2];
     
    System.out.println("The maximum number is " + first);
    System.out.println("The second number is " + second);

    Bye

Similar Threads

  1. Java Applet Coding Help?
    By Drag01 in forum Java Applets
    Replies: 1
    Last Post: April 26th, 2012, 07:03 AM
  2. NEED SERIOUS HELP CODING THIS PROGRAMMING ASAP
    By remy27 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 20th, 2010, 03:03 PM
  3. Help me in java coding
    By smallmac in forum Java Theory & Questions
    Replies: 5
    Last Post: August 2nd, 2010, 09:50 AM
  4. Java coding help
    By Javalover1 in forum The Cafe
    Replies: 0
    Last Post: April 12th, 2010, 08:11 PM
  5. Sudoku coding help...please
    By sketch_flygirl in forum Algorithms & Recursion
    Replies: 2
    Last Post: December 5th, 2009, 10:07 PM