English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have to add all the ascii values of a 3 character string. Can you help me out? i need it in java. I assume theres some easy way to do it, but i cant find it in our text book.

2007-10-16 03:10:03 · 3 answers · asked by buckeyefan1191 3 in Computers & Internet Programming & Design

That code will only work with a string that is just integers wont it. Im looking for the ASCII values. like i know that A is equal to 101 and Z is equal to 132.
I just dont know how to get java to do this for me without a ton of if then statements.

2007-10-16 03:32:10 · update #1

3 answers

String a = "123";

System.out.println(
Integer.parseInt(a.substring(0,1)) +
Integer.parseInt(a.substring(1,2)) +
Integer.parseInt(a.substring(2,3)));

Prints '6'.

---------------------------------------

Have just read your additional details...

String a="abc";
int tot=0;

for (int i=0; i {
tot+=a.charAt(i);
}

System.out.println("Total is "+tot);

Bear in mind char is multibyte aware. 'a' may not always be '97'.


To the other answerer, 101 octal = 65 dec = 41 hex = 'A' in ascii.

2007-10-16 03:24:16 · answer #1 · answered by Bob R 4 · 0 0

Convert the form to a string. Parse the string. for each character, if this is 0, increment the 0 variable. Else if mod 2 of the fee of the character is 0 this is even, in the different case this is unusual.

2016-12-18 08:59:32 · answer #2 · answered by maiale 4 · 0 0

I think this is the nearest I can get. It seems about right.
http://java.sun.com/javase/6/docs/api/java/lang/Character.html#getNumericValue(char)

To convert a string to an array of chars use
http://java.sun.com/javase/6/docs/api/java/lang/String.html#toCharArray()

I don't know what book you are using for ASCII codes but Upper case A is dec 65 and hex 41
Upper case Z is dec 90 and hex 5A
Lower case a is dec 97 and hex 61
Lower case z is dec 122 and hex 7A

2007-10-16 03:51:55 · answer #3 · answered by AnalProgrammer 7 · 0 0

fedest.com, questions and answers