Last active
October 23, 2020 01:15
-
-
Save zeraf29/774335da8d78f23479c4c2c0b04ebbf6 to your computer and use it in GitHub Desktop.
get Highest and Lowest number from string of space separated numbers, using "for" statement
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static String HighAndLow(String numbers) { | |
// Code here or | |
String[] splitStr = numbers.split(" "); // split by " " | |
int lNum = Integer.parseInt(splitStr[0]); //lowest number init; | |
int hNum = Integer.parseInt(splitStr[0]); // highest number init; | |
for(int i=1; i<splitStr.length; i++){//compare | |
if(lNum>Integer.parseInt(splitStr[i])) | |
lNum = Integer.parseInt(splitStr[i]); | |
if(hNum<Integer.parseInt(splitStr[i])) | |
hNum = Integer.parseInt(splitStr[i]); | |
} | |
//"hNum lNum"; | |
return String.valueOf(hNum)+" "+ String.valueOf(lNum); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
//Using java8
public static String HighAndLow(String numbers) {