0

Hello I need to make a program that has the user enter a name(for example) and find the greatest value ascii character within the string then output it to the user. I've began it but I'm not sure where to continue. Thanks for your help.

System.out.println("Enter your name: ");
String name = input.nextLine();
char ch = ' ';
int i;

for (i = 0; i < name.length(); i++)
{

}
Adam Bunch
  • 113
  • 1
  • 2
  • 14
  • 1
    you want to start at the first character and compare the next one to this. if the second is larger then you want to use this as the base moving forward – tmaxxcar Feb 11 '16 at 22:47
  • 2
    There is no such thing as an "ascii character"; ASCII is a character coding. What you want is the highest code point. With Java 8 it's as simple as `theString.codePoints().max().orElse(-1)` – fge Feb 11 '16 at 22:50
  • @fge: A bit off topic: There is of course ASCII character (depending on how you interpret the term of course). ASCII is a character encoding scheme and a valid "ASCII character" can simply means a value that exist in the standard of ASCII. Although it is more meaningful to deal with code point in Java given that it is built on Unicode (which is just another character encoding scheme), it is still valid to deal with ASCII. Given that Unicode code point is compatible with ASCII (yes it is. The "basic" ASCII only defines up to 7 bits) with codepoints < 128, the question can be interpreted as – Adrian Shum Feb 12 '16 at 08:28
  • Find the maximum character inputted by user, for which the character is < 128 – Adrian Shum Feb 12 '16 at 08:28
  • @AdrianShum no, Unicode is not a character coding! It is a list of code points; it _does_ define character codings, though (the UTF-* series; UTF means Unicode Transformation Format) – fge Feb 12 '16 at 08:53
  • please note what I said: it is a "character encoding scheme". Both ASCII and Unicode (and EBCIDIC, Extended ASCII, Big-5, Shift-JIS etc are all character encoding schemes) – Adrian Shum Feb 12 '16 at 08:55
  • @AdrianShum well, no. Unicode is _independent of character coding_. You don't get Unicode to tell you "this character is serialized as this or that in a byte stream". Yes, I'm picky about such things :p – fge Feb 12 '16 at 09:00
  • Please forgive me, I doubt if "character coding" is a valid term though. I believe you have kind of mixed up the relationship among Unicode, UTF (which is a mapping of Unicode codepoint to byte sequence) and character encoding schemes (which represents a character with an integer value, for which Unicode is one way of doing so, and there is other standards much older than Unicode doing such work, such as ASCII). And I am picky about such things too, that's why I am saying you are wrong :) – Adrian Shum Feb 12 '16 at 09:02
  • @AdrianShum and I personally believe that _you_ are mixed up; a character coding is the mechanism by which a glyph is encoded to a sequence of bytes. Unicode defines the glyphs; the _character coding_ defines how those glyphs are translated. – fge Feb 12 '16 at 18:04
  • @fge: That's why I said you are mixed up: Unicode consists of 2 part: 1. A mapping of Glyph to a value (actually it is more appropriate to use Character here instead of Glyph) and 2, How you translate the glyph-mapped-value to byte sequence (which we usually referred as Unicode encoding), hence the **UTF** series of standard. – Adrian Shum Feb 15 '16 at 01:37
  • ASCII (and most other character encoding scheme) does not have clear distinction between 1 & 2 though. However, it is simply not appropriate to say "ASCII" is simply one of the "Unicode encodings" just because Unicode is being a superset of ASCII. It is totally possible that a character encoding scheme is containing a different set of character as in Unicode. – Adrian Shum Feb 15 '16 at 01:42
  • and most important is, ASCII can never serve as a, in your terminology, "character coding" for Unicode. – Adrian Shum Feb 15 '16 at 01:51

4 Answers4

2
String name = "Just for Testing";
int greatestVal = 0;

for (int i = 0; i < name.length(); i++)
{
    int curVal = (int)name.charAt(i);
    if(curVal > greatestVal)
        greatestVal = curVal;
}
char asChar = (char)greatestVal;
System.out.println("The character with the highest ASCII (encoding) number was " + asChar + " (with ASCII (encoding) " + greatestVal + ")");

Now to explain this a little bit:

What happens at the line int curVal = (int)name.charAt(i); is called explicit casting. .charAt() returns a char, which is casted by us to an int (by using (int) in front of it). This is where "the magic" happens. curVal now contains the corresponding value for the character at position i in the platform's default encoding (for more infos see this post). What nearly all of those encodings have in common is that the first 128 characters match to ASCII, more details in this question.

This can now be compared with greatestVal, and by looping over the whole String, we can find the character with the highest underlying ASCII value. Here a link to an ASCII table, so you can check yourself which character corresponds to which value.

At char asChar = (char)greatestVal; we just do the same again, we cast the int back to a char, which may then be printed.


As shown in another answer, you can also compare chars to each other, like name.charAt(i) > greatestChar. What happens behind is that those two characters are compared based on their encoding value, just as I showed you manually in the above example.

Markus Weninger
  • 11,931
  • 7
  • 64
  • 137
  • 1
    Java's `char`, `Character` and `string` use Unicode/UTF-16. Instead of ASCII, here's a link to a [Unicode table](http://unicode.org/charts/nameslist/). (Note values are in hexadecimal per convention.) – Tom Blodget Feb 12 '16 at 00:21
  • So, updated it and added links to two other questions dealing with encodings. – Markus Weninger Feb 12 '16 at 07:57
  • 1
    You did some good research on this but it's simpler than that. Java (like JavaScript, .NET, …) generally use Unicode throughout; UTF-16 in memory and UTF-8 for streams and files. `charAt` gives the UTF-16 code unit at the index. The platform default encoding can sneak into a few methods but it's best to avoid those. In the specialized cases where Unicode is not desired, a specific encoding is usually prefered over "platform default". ASCII is seldom the platform default. – Tom Blodget Feb 12 '16 at 15:22
  • 1
    The conversion from `char` to `int` is not necessary. As there will be an implicit [widening primitive conversion jls 5.1.2](https://docs.oracle.com/javase/specs/jls/se9/html/jls-5.html#jls-5.1.2). So you can directly make a comparison as `currentCahr > greatestChar` where are both variables of type `char`. – SubOptimal Dec 05 '17 at 09:28
1

You might start with this snippet

// the String to analyze
String name = "foobar";
// use the lowest character as initial value
char greatestChar = (char) 0;
// iterate over all characters in the string
for (int i = 0; i < name.length(); i++) {
    // if the current character is bigger then the actual stored greatest ...
    if (name.charAt(i) > greatestChar) {
        // ... then keep the actual one as greatest
        greatestChar = name.charAt(i);
    }
}
System.out.println("greatestChar = " + greatestChar);

edit A snippet to use the Stream API.

String name = "foobar";
char greatestChar = (char) name.chars().max().getAsInt();
    // .chars() - returns a stream of int
    // .max() - return the maximum element in the stream (which is an OptionalInt)
    // .getAsInt() - return the value of the OptionalInt as int
System.out.println("greatestChar = " + greatestChar);
SubOptimal
  • 22,518
  • 3
  • 53
  • 69
1

Something like this would work

String str = "someString"; 
char[] charArray = str.toCharArray();
int max = 0;
for(char c : charArray){
    max = Math.max(max, (int) c);
}
System.out.println("Value: " + max);
System.out.println(Character.toString((char) max));
Viktor Mellgren
  • 4,318
  • 3
  • 42
  • 75
0

In Java 8+ this can be done pretty simple with a one-liner:

String str = "TestString"; // Expected result: 'S'
char largestChar = (char)str.chars().min().getAsInt();

Or if you want to print it:

String str = "TestString"; // Expected result: 'S'
System.out.printf("Largest ASCII-value is: '%c'", str.chars().min().getAsInt());

Try it online.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135