1

I'm trying to convert an array of characters into integers, so I can get the ascii code, but the code I have doesn't seem to be working.

import javax.swing.*;
import java.text.*;
import java.util.*;
import java.lang.*;

public class Encrypt {


public static void main(String[] args) {

String phrase = JOptionPane.showInputDialog(null, "Enter phrase to be messed with ");
char[] charArray = phrase.toCharArray();

for (int count = 0; count < charArray.length; count++) {
int digit = ((int)charArray[count]);

System.out.println(digit[count]);

}




}

3 Answers 3

2

You don't need to explicitly cast it. You may simply assign it to int. Refer 5.1.2. Widening Primitive Conversion

Example:

 char[] charArray = "test".toCharArray();

        for (int count = 0; count < charArray.length; count++) {
            int digit = charArray[count];

            System.out.println(digit);

        }

output:

116
101
115
116
Sign up to request clarification or add additional context in comments.

Comments

2

digit is an int type primitive variable can't be treated as an array

digit[count])

just use

digit

Comments

0

Try it this way...

char[] charArray = phrase.toCharArray();

int[] intArray = new int[charArray.length];

int i=0;

for (char c : charArray){

      intArray[i] = c;
      i++;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.