write java code to print second max number in the array

Answer Posted / rajaram

public static void main(String[] args){
int maxNumber = 0;
int secondMaxNumber = 0;
if(args.length == 0){
System.err.println("Number array is empty");
return;
}
for(int i=0; i < args.length; i++){
int currNumber = Integer.parseInt(args[i]);
if(maxNumber < currNumber){
secondMaxNumber = maxNumber;
maxNumber = currNumber;
}else if(secondMaxNumber < currNumber){
secondMaxNumber = currNumber;
}
}
System.err.println("Max. number is "+maxNumber);
System.err.println("Second Max. is "+secondMaxNumber);
}
}

Is This Answer Correct ?    28 Yes 8 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

What is import java util arraylist?

661


What is the difference between Error, defect,fault, failure and mistake?

661


take an array with -ve and +ve value both.find out the nearest value of 0(zero).if two values are same like(-2 and +2)then extract +2 is nearest of 0(zero).

1533


Given a singly linked list, find the middle of the list in a single traversal without using temporary variable.

612


Can we have try block without catch block?

588






Can we create an object of static class in java?

598


What are the procedures?

610


Are generics important java?

526


Can we use static class instead of singleton?

582


What is difference between == and === in js?

551


What is an accessor?

1053


Why main function is static?

674


Given a singly linked list, determine whether it contains a loop or not without using temporary space?

583


Is integer passed by reference in java?

570


What is final keyword in java?

555