Hi, I am having a bit of trouble finding a way to check a string for the highest and lowest number. Example:

I would have a:

String example = "1 3 5 9 4 3";

And now I would have to check which is the lowest number and the highest too. Maybe with a for and auxiliary variables like:

int highest;
int lowest;

I can't figure out a way to do it...Furthermore, the numbers must be spaced like above, and I got no clue on how to take them into a int or something... And it can't be using arrays since it is a school project and the teacher won't let us use them in any way...

If you could help me I would be most grateful.

  • :$

Dani AI

Generated

Quick clarification and an alternative approach that fits the “no arrays” constraint: was right that a left‑to‑right scan works, and ’s Scanner solution is the simplest in practice. If the assignment forbids arrays (and you want to avoid using Scanner or built-in tokenizers), parse the string character‑by‑character, build each integer as you go, and update running min/max variables. The code below is a compact, self‑contained example that handles multiple spaces, negative numbers and very large numeric tokens (it clamps overflow to int limits rather than throwing).

public static void printMinMax(String s) {
    if (s == null || s.trim().isEmpty()) {
        System.out.println("No numbers found");
        return;
    }
    int min = Integer.MAX_VALUE;
    int max = Integer.MIN_VALUE;
    int i = 0, n = s.length();
    while (i < n) {
        while (i < n && s.charAt(i) == ' ') i++;                // skip spaces
        if (i >= n) break;
        boolean neg = false;
        if (s.charAt(i) == '-') { neg = true; i++; }             // optional sign
        if (i >= n || s.charAt(i) < '0' || s.charAt(i) > '9') {  // bad token
            while (i < n && s.charAt(i) != ' ') i++;
            continue;
        }
        long val = 0;
        while (i < n && s.charAt(i) >= '0' && s.charAt(i) <= '9') {
            val = val * 10 + (s.charAt(i) - '0');
            if (val > (long) Integer.MAX_VALUE + 1) {           // prevent runaway
                while (i < n && s.charAt(i) >= '0' && s.charAt(i) <= '9') i++;
                break;
            }
            i++;
        }
        int num = neg ? (val > (long) Integer.MAX_VALUE + 1 ? Integer.MIN_VALUE : (int) -val)
                      : (val > Integer.MAX_VALUE ? Integer.MAX_VALUE : (int) val);
        if (num < min) min = num;
        if (num > max) max = num;
    }
    if (min == Integer.MAX_VALUE && max == Integer.MIN_VALUE)
        System.out.println("No valid integers found");
    else
        System.out.println("Min: " + min + ", Max: " + max);
}

Notes and troubleshooting

  • Initialize extremes to Integer.MAX_VALUE / Integer.MIN_VALUE so the first parsed number sets them correctly.
  • The parser skips malformed tokens instead of failing; change that to throw an error if strict input is required.
  • If separators other than simple spaces (tabs, commas) appear, either normalize them first or extend the skip logic.
  • For homework constraints, this avoids arrays, substring/token splits, and uses only primitive variables and simple char inspection — useful when libraries or array returns are disallowed.

Recommended Answers

All 2 Replies

Use a for loop to iterate through the string from left to right until you find a whitespace character(java.lang.Character.isWhitespace(int CodePoint))

Once that condition has been satisfied use a variable whose initial value would be zero and then use the (java.lang.String.substring(int beginIndex, int endIndex)) to extract the first number(still as a string), then set the variable I just mentioned to the index value of the white space, that way when the for loop continues and you use the substring method it will extract the next number and not the entire string from the beginning.

Now in order to get the extracted numbers that are still strings to integer format use the (java.lang.Integer.parseInt(String s)) method than use a simple comparison to check if its the highest or lowest value. Keep in mind that this conversion and comparison would take place within the for loop i mentioned earlier.

Hope that helps let me know if you still need help.

thanks a lot for the insight, I got it to work with Scanning. Finished the project in one hour :P

Thanks again for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.