Results 1 to 9 of 9
  1. #1
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Post Problems with writing String values to a text file

    Currently I am working on a project that prompts users to enter a question in a text field, then looks for the question in a series of text files, then prints out an answer. At this point, I am working on a login JFrame that takes the users data and stores it in a text file. However, when I tell it to print the information to the text file, it only prints blank spaces or just "null null". I put the String user eqal to (username+" "+password); and then called the method when adding the users information into the text file. This is the method I am using when trying to add the user:

    Java Code:
    public void addUser() throws IOException
        {
        String newuser;
        newuser = user.getUser();
        NamesOutput.printf("%s" + "%n", newuser);
        NamesOutput.close();
        }
    I was able to do it before, but then I aded if/else statements to try and make the program more robust and it stopped working right. Since then I have added more to my code so it's harder to find where the error is. If anyone has any ideas on what I'm doing wrong, or need more information, please let me know. Thanks in advance ^.^

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Try debugging by printing out the value of newuser:
    System.out.println("newuser=" + newuser + "<");

    What values can the getUser() method return? Can they be null or empty?

    Look at the format for the printf method. Is your syntax correct?

  3. #3
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    The getUser() method only returns String values which is a combination of two Strings that is from a text field, data entered by the user. Both String values are set as private, the String user, which is what the method returns, isn't. Apparently, when I sent the output to the system, it gave me nothing along with the output to the text file. What could cause this?

    Java Code:
    public String getUserName()
        {
        username = first.getText();
        return username;
        }
        //-----------------------------------------------------------------
        public String getPassword()
        {
        password = passwordfield.getText();
        return password;
        }
        //-----------------------------------------------------------------
        public String getUser() throws IOException
        {
        String user = (username+" "+password);
        return user;
    Do I have to set their values in get user, like... spassword = getPassword();?

  4. #4
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Well, I went back into my code and called the method getUser(); from the class it is in, and I get the desired output. However, when I do the same from my addUser(); method, it only gives me blank spaces. Although I'm getting closer, I still have no idea why it keeps doing this to me! I know it's printing to the text file as it adds blank spaces, but not the username and password! :mad:

  5. #5
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Nevermind, I finally was able to solve my problem. I set the return variable "user" as a public static variable and then instantiated the variable in each method by calling the return method for the proper String values. I hope that this will help anyone else that had this problem when writing to a text file. Thanks for all of your help!

  6. #6
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    Making a variable static to get around a logic or design problem is NOT the way to do it.
    And it has nothing to do with writing to a text file.

  7. #7
    Tedward is offline Member
    Join Date
    May 2011
    Posts
    10
    Rep Power
    0

    Default

    Well, it kept on coming up as null or nothing when I tried to write the String to the text file. When I set the variable as a public static String it was able to print the String to the text file. Was there another way I could have fixed the problem?

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,608
    Rep Power
    23

    Default

    yes, there probably are several different ways.

  9. #9
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default

    just some advice look at the properties or hashtable classes for your program. Much easier to write to a txt file.

Similar Threads

  1. Writing into a text file
    By africanhacker in forum New To Java
    Replies: 3
    Last Post: 03-31-2011, 10:44 PM
  2. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  3. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-07-2010, 08:19 AM
  4. Writing a long string to a text file
    By jessie in forum New To Java
    Replies: 2
    Last Post: 11-06-2010, 04:44 PM
  5. Replies: 1
    Last Post: 02-27-2010, 01:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •