You want something like this :
int getIntGrade(char letter){
if(letter == 'A' || letter == 'a') return 95;
if(letter == 'B' || letter == 'b') return 85;
//and so on
}
And if you want to go the other way it would look something like this.
char getLetterGrade(int grade){
if(grade > 90) return 'A';
else if(grade > 80) return 'B';
//and so on
}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
If I understand correctly, your input is a type String. Then you want to go through each character in the string to count number of each characters (not include any number character)?
Your algorithm is odd. You tend to treat upper and lower case the same. If you are counting just to see what character is in a string, it would be fine. If you have to check for the case of the letter, your algorithm needs to be modified.
Given your code portion and I am not going to change it even though it does not mean that it is the best way to deal with the problem, here is my sample code for you.
// sample class with static method for parsing a character to int
public class CharLetter {
// Return int value of a character which is supposed to be capital letters,
// and its value would be between 1 to 26 (A to Z)
public static int parseInt(char c) {
int charInt = (int) c;
if (charInt>=97 && charInt<=122) { charInt -= 32; } // convert lower to upper
return (charInt - 64); // convert the ASCII value of a capital letter to int
}
}
// when use in the code
String str = "anInput 2 String";
int pos = 1;
int intLetter; // no check for str here because of hard-coded
while (pos<str.length()) {
if (intLetter>=1 && intLetter<=26) {
intLetter= CharLetter.parseInt(str.charAt(pos));
// do something about the count
pos++; // move to the next character in the string
}
else {
// do whatever you want here with any character that is not a letter
}
}
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
if (charInt>=97 && charInt<=122) { charInt -= 32; } // convert lower to upper
if (intLetter>=1 && intLetter<=26)
When will people ever learn? Of all the world's many languages, English is just about the only one that uses a 26 letter alphabet. Even Spanish and French have accented characters with upper and lower case variants, and German has a commonly used additional letter that is not in the 26.
This kind of code, that assumes US ASCII 7 bit coding, has no place in Java, which is UNICODE.
Please use the methods that the Java developers have provided to ensure some kind of international correctness.
Finally. nobody has actually mentioned the key point for the OP - char is aninteger data type. You can simply assign a char to an int without needing to convert anything.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
This kind of code, that assumes US ASCII 7 bit coding, has no place in Java, which is UNICODE.
And I already stated that I would try not to change the way he dealt with his problem. I just showed how to do it for his own way.
PS: His assignment does NOT give any extra credit for dealing the problem that deep. Simple way to solve the problem with assumptions is implied in the assignment.
Taywin
Posting Virtuoso
1,727 posts since Apr 2010
Reputation Points: 229
Solved Threads: 239
Yes, if the brief is to count the letters then the isLetter method is the "correct" way to go.
The 97's and 'a'-1 stuff is a dead end. It only works for the 26 non-accented English letters and even if that's enough for this exercise, it's definitely not enough for anything real. As an algorithm it doesn't scale up either. As long as you know that chars are numbers, and each letter is represented by a different number defined by the UNICODE system, that's enough.
JamesCherrill
Posting Genius
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
If the purpose of the assignment is to learn to use the library methods, then using the library methods is the right way to go. If the purpose of the assignment is to learn what lies behind the library methods, then you have to do something more like the K&R-type solutions (although none of the ones suggested really move me to stand up and cheer).
Depends what the professor is trying to teach you. I always assume that since using the library methods is trivial (you look them up and apply them - you need to go to school for this?) the avoidance of library methods is implicit in any intro CS assignment.
In this case, I see nothing wrong with writing a 26-character solution based on the English version of the Roman alphabet, using only primitive code (except for some String method to extract characters, of course). This seems like a fine sort of assignment for a first-year course. The follow-on assignment might easily be: deal with Spanish. Then, deal with devanagari. And then you have a student who understands something about unicode because they've had to tangle with it barehanded - and someone who knows why the library methods exist, and why they should be treated with some respect.
To the original poster, I'd say this: James is certainly correct to say that in production code, you'd never write your own character manipulation of this sort, but if you're going to be writing production code, you certainly should understand how this sort of character manipulation works. Whatever you end up writing for your class, you should make sure that you understand why each of your respondents offered you the code and ideas they offered you, what problems those solve, and how they work. And, how they could work better. You'll never write that in your working life, but that doesn't mean you won't need to know how they work.
jon.kiparsky
Posting Virtuoso
1,849 posts since Jun 2010
Reputation Points: 383
Solved Threads: 187