0

I need to convert a String to an array with objects (one object for each char of the string).

I have this code:

public void inputPin(String pin)
    {
        char[] charArray = pin.toCharArray();
        for(int i = 0;i < charArray.length;i++)
        {

        }

    }

I need to place inside the for loop a method like pressButton() and this method will receive as a parameter a String (that is one of the characters of the pin).

How can I get each of the characters of the array and use it as a parameter of the pressButton() method?

5
  • 4
    No ideas. The question is not clear. Not the least bit... Commented Sep 18, 2013 at 13:56
  • See stackoverflow.com/questions/3413586/… Commented Sep 18, 2013 at 13:57
  • Why not just get the size of the string and then create an array of objects based on that size? Commented Sep 18, 2013 at 13:58
  • So...you're looking to a solution to cram into the for loop? Commented Sep 18, 2013 at 13:58
  • Yes, I need a solution to cram into the for loop. Commented Sep 18, 2013 at 14:31

5 Answers 5

1

Try this

String pin = "Sample";
char[] charArray = pin.toCharArray();
for(char c : charArray)
{
    pressButton(""+c); // to convert char to String
}
Sign up to request clarification or add additional context in comments.

2 Comments

It's issue with other point, just try to print all characters, give your detail code so it can be identify.
I had a method calling to itself, that was the error. Finally your code worked fine. Thanks
1

You can get array of symbols using split

pin.split("");

split will return array of String objects - you can convert it to char if you want

Comments

1

To convert a char into a String, use

String.valueOf([your char here])

You can also do it by using concatenation so

[yourchar] + ""

will return a String, too.

Comments

0

Looking for something like this to get an array of Character object from resulting char array:

char[] charArray = pin.toCharArray();
Character[] objArray = new Character[charArray.length];
for (int i = 0; i < charArray.length; i++) {
    objArray[i] = new Character(charArray[i]);

Comments

0

try this

    char[] charArray = pin.toCharArray();
    Character[] characterArray = new Character[charArray.length]; 
    for (int i = 0; i < charArray.length; i++) {
        characterArray[i] = charArray[i];
    }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.