0

Hi all, am writing a programming for my assignment.

Got stuck in my codes for a small portion.

Currently i have trying to extract all the alphabets within a string and cover them to an ascII.

For example:
I have a string "1a2b3c"
What i need to do is to read through this string, if i do see any letters a-z or A-Z, i should take it out, covert it into ASCii number.

I know how to convert my letters to ASCII. I just cant figure out how to extract the letters when i see them and compare them..

Can someone help me please?

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by AcidG3rm5
0

The String class has the charAt method. "Loop" the String using its length and get each character of the String.

Then if you take that character and put it into an int, you have the ASCII:

for-loop {
char ch = yourString.charAt(i);
int ascii = ch;
System.out.println(ascii);
}

Edited by javaAddict: n/a

0

Thanks. Have got the code working..

String temp=null, temp2;
		int ascII=0;
		for (int i=0; i<hash.length();i++)
		{
			if (Character.isDigit(hash.charAt(i)))
			{
				if(temp==null)
				{
					temp = Character.toString(hash.charAt(i));
				}
				else if (temp.length()>0)
				{
					temp = temp + Character.toString(hash.charAt(i));
				}
			}

			else if (Character.isLetter(hash.charAt(i)))
			{
				if(temp==null)
				{
					ascII = (int)hash.charAt(i);
					temp2 = Integer.toString(ascII);

					temp = temp2;
				}

				else if (temp.length()>0)
				{
					ascII = (int)hash.charAt(i);
					temp2 = Integer.toString(ascII);
					temp = temp + temp2;
				}

			}

		}//end for
This question has already been answered. 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.