I'm having trouble in my assignment
Assignment :: TwoLargestElements
Complete the following program so that it computes and writes out the two largest elements in the array.
It always display "12" on both largest and largest2
import java.io.* ;
public class TwoLargest {
public static void main ( String[] args )
throws IOException {
int[] data = {3, 1, 5, 7, 4, 12, -3, 8, -2};
int largest = 0, largest2 = 0;
for ( int index = 0; index < data.length; index++)
if(largest < data[index]) {
largest = data [index];
}
for ( int index = 0; index < data.length; index++)
if (largest2 < largest) {
largest2 = largest;
}
System.out.println("The largest number in the array is: " + largest);
System.out.println("The second largest number in the array is: " + largest2);
}
}