write java code to print second max number in the array
Answer Posted / raja ram
public class SecondMaximumNumber{
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. number is
"+secondMaxNumber);
}
}
Is This Answer Correct ? | 7 Yes | 5 No |
Post New Answer View All Answers
What is string pooling concept?
What are synchronized blocks in java?
How do you override a method?
Is integer immutable in java?
Is char a data type in java?
Explain a few methods of overloading best practices in java?
What about main() method in java ?
What will happen if non-synchronized method calls a static synchronized method and what kind of lock it acquires?
What is final?
Is null an object java?
How to write custom exception in java?
In how many ways we can create threads in java?
What is a jit compiler?
What is the use of parseint in java?
Define reflection.