'How do I create an array of chars in reverse order in Java? [duplicate]

I tried the following, but I always get an error message. ("Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 10")

public class rechner2 {
    static String a = "7289347928";

    
    public static void main(String[] args) {
        for (int i = 0; i < a.length(); i++) {
            System.out.println(langeZahl(a)[i]);
        }
    }
    
    
    private static char[] langeZahl(String a) {
         if (a == null) {
             return null;
         }

         int len = a.length();
         char[] array = new char[len];
         for (int i = 0; i <= len; i++) {
             array[i] = a.charAt(a.length() - i);
         }
         return array;
    }
}

I don't get it.



Solution 1:[1]

your error is in your for. In your first for you use i < a.length() but in ypur second for you use i <= len and you will need another 'counter' to keep the position of the 'a' array you are getting the chars from. The fixed code will be this one:

static String a = "7289347928";

public static void main(String[] args) {
    for (int i = 0; i < a.length(); i++) 
    {
        System.out.println(langeZahl(a)[i]);
    }
}


private static char[] langeZahl(String a) {
     if (a == null) {
         return null;
     }

     int len = a.length();
     int j = len-1;
     char[] array = new char[len];
     for (int i = 0; i < len; i++) {
         array[i] = a.charAt(j);
         j--;
     }
     
     return array;
}

Solution 2:[2]

There's no automatic conversion from a file path, or file contents, to an RSAParameters; the RSACryptoServiceProvider is out of date and not recommended for new code, and the PrivateKey property on certificates is fully [Obsolete] in new versions of .NET.

With .NET 5+, this is easy:

byte[] pfxBytes;

using (X509Certificate2 cert = X509Certificate2.CreateFromPemFile(certFile, keyFile))
{
    pfxBytes = cert.Export(X509ContentType.Pkcs12, pfxPwd);
}

Or, in the style closer to the code you've written:

byte[] pfxBytes;

using (X509Certificate2 cert = new X509Certificate2(certFile))
using (RSA key = RSA.Create())
{
    key.ImportFromPem(File.ReadAllText(keyFile));

    using (X509Certificate2 certWithKey = cert.CopyWithPrivateKey(key))
    {
        pfxBytes = certWithKey.Export(X509ContentType.Pkcs12, pfxPwd);
    }
}

Your reference snippet then goes on to ignore the PFX/PKCS12 output and pass the cert to RestAsynchronicClient. Because of some idiosyncrasies on Windows, that generally won't work if you load the cert from this style. But, if you load the PFX into a new X509Certificate2 object, that'll be in a slightly different state and everything'll be happy.

RestAsynchronicClient client = new RestAsynchronicClient(
    url,
    RestDataStandard.JSON,
    null,
    new X509Certificate2(pfxBytes, pfxPwd),
    logger);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Willian Valer
Solution 2 bartonjs