philosophical introvert philosophical introvert - 8 months ago 24
Java Question

How to get the max/min value of a String[] array?

I got the idea to check for the max/min value of an array from here. It works wonders, but I'm wondering if there's a way to tear the code apart and make it more compact. Maybe there's a command of some sort? I don't know, and that's why I'm asking you.

public static void main(String[] args) {
String[] input = JOptionPane.showInputDialog("What are you grades this month?\nThey must be seperated by spaces.").split(" ");

int smallest = Integer.parseInt(input[0]);
int largest = Integer.parseInt(input[0]);

for (int i = 1; i < input.length; i++) {
if (Integer.parseInt(input[i]) > largest) {
largest = Integer.parseInt(input[i]);
else if (Integer.parseInt(input[i]) < smallest) {
smallest = Integer.parseInt(input[i]);


This example will help you simplify your code

Integer [] myIntArray = new Integer[]{0, 5, 2, 11};

//Output smallest 

//Output largest
System.out.println(myIntArray[myIntArray.length - 1]);