0

For the function below

public static String symbolArrayToString(int[] symbols) { 

  String message = "";

    for (int i = 0; i < symbols.length; i++) {

        symbols[i] = message.codePointAt(i)-65;
        message = message + Integer.toString(symbols[i]);

        message.toUpperCase();

    }
    System.out.println(message);

    return message; 
}

How would you convert the int array to chars before it becomes a String? I keep on either getting this error:

java.lang.StringIndexOutOfBoundsException: String index out of range: 0
    at java.lang.String.codePointAt(Unknown Source)
    at Caesar.symbolArrayToString(Caesar.java:50)
    at Caesar.main(Caesar.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at   edu.rice.cs.drjava.model.compiler.JavacCompiler.runCommand
      (JavacCompiler.java:272)

Or it just returns the numbers of the array in a String.

1
  • 2
    message is empty. but you are trying message.codePointAt(i). Commented Feb 20, 2016 at 6:00

2 Answers 2

1

Try changing the line with .codePointAt to work with the array itself. Try this:

public static String symbolArrayToString(int[] symbols) { 

String message = "";

for (int i = 0; i < symbols.length; i++) {

    symbols[i] = symbols[i] - 65;
    message = message + Integer.toString(symbols[i]);

    message.toUpperCase();

}
System.out.println(message);

return message; 

}

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

Comments

1

You can get the character by using this :

Character.toChars(symbols[i])

So your code should be like :

for (int i = 0; i < symbols.length; i++) {
        message = message + String.valueOf(Character.toChars(symbols[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.