1 Attachment(s)
Save Textfield to an Array
I have a crossword generator project, and the user is going to list in their words in text fields to generate a crossword puzzle from those words.
I need to write a proper code to take in the words from the textfields and save them in an array.
and then save them to a text field. to make it easier for the user to look at his word when he comes back to the application.
I wrote A code, but it doesnt seem to be appropriate because everytime I try to print the array contents it prints nothing.
I might need help with the line that takes the text from a text field and saves it in an array , or a new idea to do it.
I am new to java and not very fimiliar with it.
I am using Netbeans IDE
Attachment 1276Attachment 1276
Re: Save Textfield to an Array
Hello NorahPoly!
Can you post the code you are having trouble with? Or better yet, a simple program that executes and shows the problem?
Re: Save Textfield to an Array
Thanks andreas for your replay,
1 Attachment(s)
Re: Save Textfield to an Array
Attachment 1277
this is my code that takes words from everytext field and save it to an array and then write it to a file, but it doesnt work.
String[] words = null;// = new String[15];
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
words[0] = word1TXT.getText();
words[1] = word2TXT.getText();
words[2] = word3TXT.getText();
words[3] = word4TXT.getText();
words[4] = word5TXT.getText();
words[5] = word6TXT.getText();
words[6] = word7TXT.getText();
words[7] = word8TXT.getText();
words[8] = word9TXT.getText();
words[9] = word10TXT.getText();
words[10] = word11TXT.getText();
words[11] = word12TXT.getText();
words[12] = word13TXT.getText();
words[13] = word14TXT.getText();
words[14] = word15TXT.getText();
try
{
PrintWriter pr = new PrintWriter("blabla");
for (int i=0; i<words.length ; i++)
{
pr.println(words[i]);
}
pr.close();
}
catch (Exception e)
{
System.out.println("No such file exists.");
}
}
Re: Save Textfield to an Array
Please explain the problem. Post the full text of any error messages.
Re: Save Textfield to an Array
okay, find the attachment.. there are 15 text fields. I need to save each word of the textfield in a new line of a .txt file. words do not save in a .txt file,, and a .txt file doesnt happen to be created when i run.
Re: Save Textfield to an Array
What is written to the file when the program executes?
Re: Save Textfield to an Array
the file cannot be created idk why
Re: Save Textfield to an Array
Are there any error messages?
You should add a call to printStackTrace in the catch block to give a full error message.
Code :
catch (Exception e)
{
System.out.println("No such file exists.");
e.printStackTrace(); //<<<< give full error message with this
}
Re: Save Textfield to an Array
I'm going to assume you are using the Swing API? If yes, then JTextField has an inherited getText() method that returns a String.
What kind of array are you wanting to save it to? A String is already an array of characters by default.