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.

  • :$

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.