Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 10 of 10

Thread: Save Textfield to an Array

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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
    Untitled.jpgUntitled.jpg


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hellas
    Posts
    284
    Thanks
    11
    Thanked 59 Times in 57 Posts

    Default 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?

  3. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save Textfield to an Array

    Thanks andreas for your replay,

  4. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save Textfield to an Array

    GUI.jpg

    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.");
    }
    }

  5. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Save Textfield to an Array

    it doesnt work.
    Please explain the problem. Post the full text of any error messages.
    If you don't understand my answer, don't ignore it, ask a question.

  6. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Save Textfield to an Array

    What is written to the file when the program executes?
    If you don't understand my answer, don't ignore it, ask a question.

  8. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Save Textfield to an Array

    the file cannot be created idk why

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default 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.
    catch (Exception e)
    {
    System.out.println("No such file exists.");
    e.printStackTrace();  //<<<< give full error message with this
    }
    If you don't understand my answer, don't ignore it, ask a question.

  10. #10
    Junior Member
    Join Date
    Jun 2012
    Location
    St. Louis, MO
    Posts
    1
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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.

Similar Threads

  1. How to save Image into a package by JButton save?
    By justyStepi in forum AWT / Java Swing
    Replies: 1
    Last Post: May 12th, 2012, 07:02 PM
  2. problem in 2D array of TextField
    By Eng.muslima in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 6th, 2011, 10:06 PM
  3. Populating a 2D array with textfield values
    By drixnak in forum What's Wrong With My Code?
    Replies: 9
    Last Post: October 19th, 2010, 01:20 PM
  4. Need Help with my JComboBox and Textfield
    By superawesome in forum AWT / Java Swing
    Replies: 1
    Last Post: November 10th, 2009, 12:15 PM
  5. Combo/TextField Help
    By GRossi0511 in forum AWT / Java Swing
    Replies: 2
    Last Post: October 26th, 2009, 08:15 PM