0

I need to compare the following characters c,d,h,s

such that

s will be the biggest
followed by h , c
and d the smallest.

d < c < h < s

I have no idea how should I implement it.
please help.

3
Contributors
4
Replies
20
Views
3 Years
Discussion Span
Last Post by rowen_1
0

create an enum, which has the value of a char as value. add the nr (of order) as a field field, use this to compare them

0

chars in Java are numeric types - they are 16 bit integers that are used to represent characters in unicode. So its pefectly OK to compare them with > or < operators. Unicode 'a' - 'z' are represented by the numeric values 97-122 respectively, so the comparisons follow normal alphabetic order, ie 'c' < 'd' < 'h' < 's'
Are you sure you want to compare 'd' < 'c'? If so, what about 'a', 'b' and 'e'?

Edited by JamesCherrill

0

Ahha! Just seen the significance of those 4 letters. stultuske had the answer: they should be an enum, although there's no need to add the number because enums have a ordinal() method that returns their position in the order in which they were declared. (And yes, you'll want another enum with 13 values.)

Edited by JamesCherrill

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.