wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Transferring string to an array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Transferring string to an array" Watch "Transferring string to an array" New topic
Author

Transferring string to an array

peter huff
Greenhorn

Joined: Nov 14, 2005
Posts: 14
Ok, I'm trying to transfer a string to an array so i can use each character separatly. I think I've managed that, now I want to output the contents of an array. However, Ive only managed to output the first character of the string.

This is the code I use to transfer string to array:


and this is the code that outputs it to the applet:


I'm guessing te problem is somewhere in here, but I can't think for the life of me where.(bit of a noobie!)

If it helps I can post the rest of the code, its not big at all.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
My guess is that its showing the last character of the String because you display every character in the String at the same x,y position.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
Oops. sorry let me retract that comment. I misread your code.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Maybe the real question here isn't about the array but what you are doing with it -- why are you using Graphics method drawChars instead of drawString or java.awt.font.TextLayout? Advancing by character width will not take into account things like kerning which make text layout more pleasing to the eye.


There is no emoticon for what I am feeling!
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

Note also that in those first three lines of code, only the last really does anything -- the char[] you allocate with "new" is discarded and replaced by a new one that you get from calling toCharArray(). There's no need to do the first allocation at all.


[Jess in Action][AskingGoodQuestions]
peter huff
Greenhorn

Joined: Nov 14, 2005
Posts: 14
im using draw.char because i want to be able to eventually change each character separatly before printing them out collectively, basicallly im trying to write an vigenere encryption applet
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by peter huff:
im using draw.char because i want to be able to eventually change each character separatly before printing them out collectively, basicallly im trying to write an vigenere encryption applet


That encryption may lead you to using a String's charAt() method as well as StringBuffer to be a mutable string, but I don't see why you have to use a char array or drawChar.
 
jQuery in Action, 2nd edition
 
subject: Transferring string to an array