Hi,
I'm in my first Java class and I am working on a problem that I could solve if I just figure out how to convert a char to an int.
i.e.
convert A to its letter equivalent with a function like this:

``intLetter = charLetter.parseInt();``

am I close? I just need to write a program that counts letters in any given phrase and if I can convert it to its letter equivelent, then my loop will be something like this:

``while(intLetter >= 1 && intLetter <= 26)``

## All 10 Replies

A char can always be converted to an int

char x = 'z';
int i = x;

as far as basic datatype are concerned you cannot apply a method to them so

intLetter = charLetter.parseInt();

is obviously wrong

commented: Short, simple, and to the point. thanks! +1

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){
else if(grade > 80) return 'B';
//and so on
}``````

I think you want to convert a letter to its equivalent number in the alphabet. Am I right?

You can do it by using this formula.

``Character.toLowerCase(ch) - 'a' + 1``

function

``````public int toInt(char ch) {
return Character.toLowerCase(ch) - 'a' + 1;
}``````

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
}
}``````
``````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 an integer data type. You can simply assign a char to an int without needing to convert anything.

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.

Wow, thank you for all the feedback! So my assignment just gives a phrase and tells me to write a program that will total the letters in the phrase. I found a method built into java called isLetter() that I might try and integrate. Has anyone used this? This seems simpler than writing all of this code just to convert a string to a letter value of some sorts.

Macobex I would like to know more about the formula you submitted, because I don't understand it. What is the "- 'a' + 1" part?

Taywin, you are absolutely right in your understanding of the problem I am to complete. I just need to count how many letters are in a given phrase. Then I have to make a seperate program with user input, and calculate how many letters are in their phrase. Because this is a java 1 class, I'm assuming I'm not going to go into a lot of depth. I'm not sure what 97, 122, 32, etc numbers are. Are these the unicode for the alphabet? Did I miss something? Sorry, I really am new to this.

James,

Please use the methods that the Java developers have provided to ensure some kind of international correctness.

I'm assuming the isLetter() method built into Java is what you're referring to? This should work for international correctness right?

Thanks for all your help everyone, please let me know if you would recommend integrating the isLetter() method instead of coding formulas for the alphabet char.

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.

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.

Thanks guys, I used the isLetter() method in my code and got a perfect score!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.