philosophical introvert - 1 year ago 49

Java Question

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]);

}

}

}

Answer Source

This example will help you simplify your code

```
Integer [] myIntArray = new Integer[]{0, 5, 2, 11};
Arrays.sort(myIntArray);
//Output smallest
System.out.println(myIntArray[0]);
//Output largest
System.out.println(myIntArray[myIntArray.length - 1]);
```