• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Transferring string to an array

 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oops. sorry let me retract that comment. I misread your code.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
peter huff
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic