By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,541 Members | 1,109 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,541 IT Pros & Developers. It's quick & easy.

REQ Assistance string to array

P: n/a
I'm trying to take a string, from a JOptionPane and change it into a
char array in which I can manipulate the characters as ASCII with
other char arrays and append into a new string. I've declared
tempArray as char[], and uPhrase is a string. this is a simple
vinere encryption program....not as easy for the beginner as it seems.
I have to take a key phrase, a test phrase and return an encrypted
phrase based on a ascii shift method.
any help is muach appreciated.

TIA
public char[] toCharArray()
{
char[] tempArray = new char[99];
tempArray = String.toCharArray(uPhrase);
return tempArray;
}

Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
registered user wrote:
public char[] toCharArray()
{
char[] tempArray = new char[99];
tempArray = String.toCharArray(uPhrase);
return tempArray;
}


As an aside, you don't need the line 'char[] tempArray = new char[99];'
tempArray is just a reference. When you call
String.toCharArray(uPhrase), the toCharArray() method is going to
initialize a new array and return a reference to it, which will be
assigned to tempArray. As a result, the 99-character array you allocated
will no longer have any references to it, and it will be garbage
collected.

Incidentally, you don't even need this toCharArray method in your class.
Just call String.toCharArray(uPhrase) in your code instead of calling
this toCharArray() method you created.

Joe

Jul 17 '05 #2

P: n/a
Joe Attardi wrote:
Incidentally, you don't even need this toCharArray method in your class.
Just call String.toCharArray(uPhrase) in your code instead of calling
this toCharArray() method you created.


Unless of course uPhrase is a private instance variable, in which case
the user-constructed toCharArray() just becomes a wrapper over that
functionality.
Jul 17 '05 #3

P: n/a
Chris Dutton wrote:
Unless of course uPhrase is a private instance variable, in which case
the user-constructed toCharArray() just becomes a wrapper over that
functionality.


Good call, Chris. I didn't think of that case.

Joe
Jul 17 '05 #4

P: n/a
Joe Attardi wrote:
Chris Dutton wrote:
Unless of course uPhrase is a private instance variable, in which case
the user-constructed toCharArray() just becomes a wrapper over that
functionality.

Good call, Chris. I didn't think of that case.


Of course, it might be that the better answer is to have the class in
question provide a toString() method, in which case it could presumably
be used tramsparently with String.toCharArray().
Jul 17 '05 #5

P: n/a
"registered user" <no**@hotmail.com> wrote in message
news:e2********************************@4ax.com...
I'm trying to take a string, from a JOptionPane and change it into a
char array in which I can manipulate the characters as ASCII with
other char arrays and append into a new string. I've declared
tempArray as char[], and uPhrase is a string. this is a simple
vinere encryption program....not as easy for the beginner as it seems.
I have to take a key phrase, a test phrase and return an encrypted
phrase based on a ascii shift method.
any help is muach appreciated.

TIA
public char[] toCharArray()
{
char[] tempArray = new char[99];
tempArray = String.toCharArray(uPhrase);
return tempArray;
}

Maybe I'm confused. What's wrong with "uPhrase.toCharArray()"? Also, unless
you know something I don't, there is no static toCharArray method in String.
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.