Skip to content
Advertisement

How do you print the highest and smallest number of ASCII value in string java

I found it, thank u mate. I actually too confuse yesterday, till i forget everything that i learnt. So here is my code, what do you think?

I just don’t know why my minChar not working when i delete this code :

JavaScript
JavaScript

Advertisement

Answer

Use this method:

JavaScript

Test case:

JavaScript

Returns

JavaScript

So what did we change?

For starters, if we are trying to get the character in the String that has the highest char int value, we don’t need n. We are looping through the String, so all we need is the length, which can already be supplied by the .length() method.

To call the method, just do:

JavaScript

EDIT: So to make the method more reliable, instead of automatically setting max to 'A', we can set it to the first char of a (e.g, a.charAt(0))

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement