-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLargestvalue.java
More file actions
38 lines (29 loc) · 1.05 KB
/
Largestvalue.java
File metadata and controls
38 lines (29 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.util.Scanner;
public class Largestvalue {
public static String findLargestString(String[] array) {
if (array == null || array.length == 0) {
return null;
}
String largestString = array[0]; // Assume the first string is the largest
for (int i = 1; i < array.length; i++) {
if (array[i].compareTo(largestString) > 0) {
largestString = array[i];
}
}
return largestString;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of strings: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consume newline
String[] array = new String[n];
System.out.println("Enter the strings:");
for (int i = 0; i < n; i++) {
array[i] = scanner.nextLine();
}
String largestString = findLargestString(array);
System.out.println("The largest string is: " + largestString);
scanner.close();
}
}