Reputation: 113
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++)
{
}
Upvotes: 0
Views: 12083
Reputation: 9326
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());
Upvotes: 0
Reputation: 12648
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 char
s 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.
Upvotes: 2
Reputation: 22973
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);
Upvotes: 1
Reputation: 4506
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));
Upvotes: 1