We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › saving text | array?
Page Index Toggle Pages: 1
saving text | array?? (Read 383 times)
saving text | array??
Oct 31st, 2008, 2:14pm
 
Hello,

i've scripted a function to write text to my applet. so when typing "a" it writes an  "A", when typing "b" it writes an "B" and so on.
Now, i want that the text one types is going to be saved, so that i can use it later on again. I want the user to type his date of birth and when pressing Enter this "text" is supposed to appear at another positon on the screen.
I don't know how to save the text one has typed. I thought of using an String Array, but i can't generelize the size of the array. (ok, date of birth is going to be always 8 letters, but another text field is going to be the name of the person). as every name has a different length i don't know how to save those.

I'd be very very glad for you help!
Thanks a lot in advance!!!
Re: saving text | array??
Reply #1 - Nov 1st, 2008, 11:20am
 
Nobody answered yet So I go...
For the record, I must mention the best structure for this data is Java's StringBuilder, allowing to add and remove characters to a string without performance/memory penalty.
Now, given that the strings are likely to be short, a pure Processing solution can be used.

You can use an Array of chars, for example. Processing offers functions like append() or shorten() to add a character at end or remove the last character (backspace).
These functions are convenient but have a cost, because on each operation a new array (bigger/smaller) is created and the content of the previous one is copied.

But unless you need to manipulate individual characters, it might be more convenient to use a simple String to hold the data typed by the user: add a character with +, remove one with substring() (it isn't in Extended Reference listing but it is in the String page).
It has roughly the same cost as with Array, duplicating the string on each transformation, but at the end you have something you can display at once.
Note that you can convert an array to a String easily, too.
Re: saving text | array??
Reply #2 - Nov 1st, 2008, 1:43pm
 
thanks for the hint to use the String class..
I've read through the help files but i always found examples like this:

String s = "Verde";
println(s.charAt(0)); // Prints "V"
println(s.charAt(2)); // Prints "r"
println(s.charAt(4)); // Prints "e"

But i did not find any help for creating an "empty" String and by typing this String is going to be filled with chars.

like this

void keyReleased() {
if (key =='a') {
text ('a', x1,x2);
}
if (key == 'b') {
text ('b', x1,x2);
}

so if i would type a - b - a - b my String s supposed to look like

String s = "abab"...

Re: saving text | array??
Reply #3 - Nov 1st, 2008, 4:42pm
 
StringBuffers are better than Strings if you're going to be modifying them a lot (better memory handling). and you can always convert a StringBuffer to a string using its toString() method.

you also don't need to check for each character individually (key == a, etc)

sun API page here:
http://java.sun.com/j2se/1.4.2/docs/api/java/lang/StringBuffer.html

Code:

StringBuffer txt = new StringBuffer();

void setup() {
}

void draw() {
}

void keyReleased() {
println("Key: " + key + "[" + keyCode + "]");
// delete pressed?
if (keyCode == 8) {
if (txt.length() > 0)
txt.deleteCharAt(txt.length() - 1);
}
} else {
// add new key
txt.append(key);
}
println("Text: [" + txt.toString() + "]");
}
Re: saving text | array??
Reply #4 - Nov 1st, 2008, 6:39pm
 
ah thanks for the code, it helps me to get to an introduction.
when using your script it prints me the keyCode of the key i've just pressed.

but no matter where i place

 println("Text: [" + txt.toString() + "]");

it is not going to be printed. as i understood it, this line is supposed to print the current word one has typed in.
but even if i delete any other println in the script, it does not work.

any idea where i made a mistake?
Re: saving text | array??
Reply #5 - Nov 1st, 2008, 7:28pm
 
no idea. that code works fine here.

the only thing i can think of is that you've moved the definition of the StringBuffer somewhere out of scope.

are you getting exceptions? error messages?

oh, wait, there's a bug -

   if (txt.length() > 0)

should be

   if (txt.length() > 0) {

with a bracket on the end. sorry!
Re: saving text | array??
Reply #6 - Nov 1st, 2008, 7:54pm
 
ah ok now it works perfectly well. Smiley thanks a lot:)
just one thing (which has no influence on the actual script). when using auto format to structure the text, i get as an error: 1024
(and heaps of red printed text with detailled information about the error.
I'm not sure whether this might be useful, so i post it here. it does influence at all the script, but maybe you have an idea what this error is all about.

anyway thanks for helping me with the StringBuffer!!!

java.lang.ArrayIndexOutOfBoundsException: 1024
at processing.app.tools.AutoFormat.getnl(AutoFormat.java:341)
at processing.app.tools.AutoFormat.show(AutoFormat.java:619)
at processing.app.Editor$20.actionPerformed(Editor.java:620)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1819)
at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.ja
va:1872)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:420)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:258)
at javax.swing.AbstractButton.doClick(AbstractButton.java:321)
at javax.swing.AbstractButton.doClick(AbstractButton.java:269)
at javax.swing.plaf.basic.BasicMenuItemUI$ClickAction.actionPerformed(BasicMenuItem
UI.java:1073)
at javax.swing.SwingUtilities.notifyAction(SwingUtilities.java:1535)
at javax.swing.JComponent.processKeyBinding(JComponent.java:2470)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:658)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:666)
at javax.swing.JMenuBar.processBindingForKeyStrokeRecursive(JMenuBar.java:666)
at javax.swing.JMenuBar.processKeyBinding(JMenuBar.java:642)
at javax.swing.KeyboardManager.fireBinding(KeyboardManager.java:253)
at javax.swing.KeyboardManager.fireKeyboardAction(KeyboardManager.java:240)
at javax.swing.JComponent.processKeyBindingsForAllComponents(JComponent.java:2547)
at javax.swing.JComponent.processKeyBindings(JComponent.java:2539)
at javax.swing.JComponent.processKeyEvent(JComponent.java:2433)
at processing.app.syntax.JEditTextArea.processKeyEvent(JEditTextArea.java:1637)
at java.awt.Component.processEvent(Component.java:4975)
at java.awt.Container.processEvent(Container.java:1613)
at java.awt.Component.dispatchEventImpl(Component.java:3681)
at java.awt.Container.dispatchEventImpl(Container.java:1671)
at java.awt.Component.dispatchEvent(Component.java:3543)
at java.awt.KeyboardFocusManager.redispatchEvent(KeyboardFocusManager.java:1713)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(DefaultKeyboardFocusManage
r.java:627)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(DefaultKeyboardFocusMan
ager.java:831)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(DefaultKeyboardFocusMan
ager.java:741)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(DefaultKeyboardFocusManager.j
ava:592)
at java.awt.Component.dispatchEventImpl(Component.java:3572)
at java.awt.Container.dispatchEventImpl(Container.java:1671)
at java.awt.Window.dispatchEventImpl(Window.java:1606)
at java.awt.Component.dispatchEvent(Component.java:3543)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:480)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:2
34)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:184
)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:178)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:170)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:100)

Page Index Toggle Pages: 1