3

How can I convert each item of a char[] (char array) to its ascii code? char_arr[i].charAt(0) doesn't work as the method doesn't exist. I can't use the toString method for char_arr[i].

import java.util.Scanner;

/**
 * Created by mona on 2/25/16.
 */
public class FunnyString {
    public static void main(String[] args){
        Scanner sc= new Scanner(System.in);

        int num_str;
        num_str=sc.nextInt();
        Boolean flag=true;

        for (int i=0;i<num_str;i++){
            StringBuilder strb  = new StringBuilder(scan.nextLine());
            StringBuilder str_reverse=strb.reverse();
            Char[] strb_arr=strb.toString().toCharArray();
            Char[] strb_rev_arr=str_reverse.toString().toCharArray();
            for (int i=1; i<strb_arr.length;i++){
                if (Math.abs(strb_arr[i]-strb_arr[i-1])!=Math.abs(strb_rev_arr[i]-strb_rev_arr[i-1])){
                    flag=false;
                }
            }
        }
        if (flag==false){
            System.out.println("Not funny");
        }
        else{
            System.out.println("Funny");
        }

    }
}
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408
  • I believe you can just cast 'int' – R.Hull Feb 26 '16 at 03:07
  • "ASCII" is the wrong term. Java strings are counted sequences of Unicode/UTF-16 code units (`Char`). Just keep in mind that real-world strings (containing [emoticons](https://en.wikipedia.org/wiki/Emoticons_(Unicode_block)) for example) are more complicated than your algorithm can handle. See the caveat for [StringBuilder.reverse](https://docs.oracle.com/javase/8/docs/api/java/lang/StringBuilder.html#reverse--). – Tom Blodget Feb 26 '16 at 08:53
  • Possible duplicate of [Convert string to ASCII value in java](http://stackoverflow.com/questions/16458564/convert-string-to-ascii-value-in-java) – rve Feb 26 '16 at 11:18

4 Answers4

1

You can try this: cast it to int

char[] chars;
for (char aChar : chars) {
    System.out.println("chars = " + (int) aChar);
}
Bahramdun Adil
  • 5,907
  • 7
  • 35
  • 68
  • That will work for all ASCII characters. It will return numbers > 127 for non-ASCII characters (but those aren't in ASCII anyway, so that's probably fine). – Thilo Feb 26 '16 at 03:10
0

Traverse through the array, assigning each char value to an int by casting, i.e.,

char_to_ascii[i] = (int) char_arr[i] ;
Thilo
  • 257,207
  • 101
  • 511
  • 656
Adrian M.
  • 360
  • 7
  • 17
0

You can use casting to integer (int) , or just add 0 to your char.

See this

char[] cs = {'a', 'b', 'c'};
for (Character c : cs)
{
    System.out.println(c + 0);
}

Output

97
98
99

See it's ascii codes of given chars http://www.asciitable.com/ .

Additionally you could get a char from a numeric value.

char[] cs = {'a', 'b', 'c'};
for (Character c : cs)
{
    System.out.println(c + 0);
    System.out.println( (char)(c + 0) );
}

Output

97
a
98
b
99
c
0

Because elements of char[] are UTF-16 code units, the standard numerical representation is in the form of backslash, "u", four hexadecimal digits. For example, 'a' is represented as '\u0061'.

Also, note that some Unicode codepoints are encoded in two UTF-16 code units. For example, the musical quarter note "" (might not display with the font your browser is using with this web page).

for (char c : "abc123".toCharArray())
{
    System.out.println(String.format("\\u%04X", (int)c));
}   
Tom Blodget
  • 20,260
  • 3
  • 39
  • 72