-7

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
  • Change `if (j == numbers.length()-1)` to `if (j == numbers.length())`. *Hint:* You just incremented the value of `j`. – Andreas Aug 11 '17 at 21:53
  • `if (numbers.charAt(j) != ' ')` is meaningless. It'll always be true. You can't get to that point in the code without `j` pointing to a space. – Andreas Aug 11 '17 at 21:54
  • Why do you do `t +=` in the `return` statement? I don't think that's right. – Andreas Aug 11 '17 at 21:56
  • @Andreas I put it in because I don't want to take the space ' ' character. I just want to take out the numbers but not the space (because here the numbers are seperated by spaces). – Duy Nghia Tran Aug 12 '17 at 09:58
  • @Andreas for the 1st comment if you don't do that you will get the OutOfBounds because you have an array. Remember that numbers.length() is always greater than the last case number of numbers because it's 0-based – Duy Nghia Tran Aug 12 '17 at 09:59
  • @Andreas For the 3rd comment I think it works that way because I want to return the string t by adding some characters to itself. But maybe I'm wrong – Duy Nghia Tran Aug 12 '17 at 10:00
  • If `j` points to the *second-last* character *(the `-` before the last `4`)*, then `t += numbers.charAt(j)` adds that minus character to `t`, and `j++` updates `j` to point to the last character, at which point `if (j == numbers.length()-1)` will `break` the loop, leaving `t` with the value `"-"`, and `Integer.parseInt(t)` will cause exactly the exception you see. If you drop the `-1` *(like I said)*, it would loop once more, adding the last `4` to `t`, so `t` will have the correct value of `"-4"`, that *can* be parsed. – Andreas Aug 12 '17 at 10:41
  • If you don't believe me, try this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/5221149) – Andreas Aug 12 '17 at 10:44

1 Answers1

2

Make this easier on yourself and use String.split().

String[] parseableNumbers = numbers.split("\\s");

This will give you an array of strings which are broken into parseable numerals. From there, you can do what you like with them. Iterating them character by character isn't going to be constructive as one number isn't exactly one character, as you've discovered.

Makoto
  • 104,088
  • 27
  • 192
  • 230
  • Hi Thanks for your help. Can I do it without using the .split because I haven't learned it in class. I just don't understand why when I put in a String like that it cannot recognize the minus sign. Basically it's the same String as if you take it from the String[] arrays no? I think what I did is like taking every numbers from the array that you created with the .split – Duy Nghia Tran Aug 12 '17 at 10:00