I want to take out the highest and the lowest number from the String that will be put as a parameter of a method.
public class main {
public static void main (String[] args){
String s = "8 3 -5 42 -1 0 0 -9 4 7 4 -4";
System.out.println(HighAndLow(s));
}
public static String HighAndLow (String numbers) {
String t = "";
int i = 0;
while (numbers.charAt(i) != ' ') {
t += numbers.charAt(i);
i++;
}
int max = Integer.parseInt(t);
int min = max;
t = "";
for (int j = i; j < numbers.length(); j++) {
if (numbers.charAt(j) != ' ') {
while (numbers.charAt(j) != ' ') {
t += numbers.charAt(j);
j++;
if (j == numbers.length()-1) {
break;
}
}
**int z = Integer.parseInt(t);** // here comes the error.
if (z > max) {
max = z;
} else if (z < min) {
min = z;
}
t = "";
}
}
return t += max + " " + min;
}
}
It says:
Exception in thread "main" java.lang.NumberFormatException: For input string: "-"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Integer.parseInt(Integer.java:572)
at java.lang.Integer.valueOf(Integer.java:766)
at main.HighAndLow(main.java:29)
at main.main(main.java:7)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
How do I solve this problem?