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?

asmundur
  • 687
  • 2
  • 7
  • 11

5 Answers5

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

sl4mmer
  • 381
  • 1
  • 12
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.

productioncoder
  • 4,107
  • 2
  • 37
  • 63
1

Try this

String pin = "Sample";
char[] charArray = pin.toCharArray();
for(char c : charArray)
{
    pressButton(""+c); // to convert char to String
}
datahaki
  • 548
  • 7
  • 21
commit
  • 4,707
  • 14
  • 41
  • 70
  • It's issue with other point, just try to print all characters, give your detail code so it can be identify. – commit Sep 18 '13 at 14:52
  • I had a method calling to itself, that was the error. Finally your code worked fine. Thanks – asmundur Sep 18 '13 at 14:55
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]);
anubhava
  • 713,503
  • 59
  • 514
  • 593
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];
    }
Evgeniy Dorofeev
  • 129,181
  • 28
  • 195
  • 266