Results 1 to 16 of 16
  1. #1
    loopsnhoops is offline Member
    Join Date
    Feb 2011
    Posts
    83
    Rep Power
    0

    Default Writing Text To A File

    I need to write a file matched with a string so it is easier to read from.

    It will write the team name matched with the points.

    Show me how please.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Look at using the Properties class. It will output lines in Key=value format.

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

    Default

    Thanks for pointing me in that direction but how can I save an int value to a key?

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Describe again what you want to do.
    I thought you wanted something like this in the file: team name(the key) and some points(the value)
    Team 1=123
    Team 4=788

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

    Default

    Exactly but it will not let me save the number as an int and I already tried to cast it but it wouldn't work. Also can the points be a variable?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    What do you want in the text file?
    I doubt that you want an int value (remember an int is a 32 bit/4 bytes value) the 4 bytes of an int would be unreadable by a human.
    Normally you convert int to String when writing to a text file.

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

    Default

    I want a hockey team and its points.
    So tell me do I use a method or what?

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Show me an exact example. Look at post#4. Is that what you want?

    do I use a method
    Yes. Everything in java is done by calling a method.

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

    Default

    Your fourth post had it spot on but how do I make a number variable get placed like that beside my key?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Convert the number to a String and concatenate the key and the new String.
    Look at the Integer class for a method to use.

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

    Default

    Oh your good.
    Thx

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

    Default

    Can you help me figure out how to get only the value of the property?

  13. #13
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Are you using a Properties file or just a plain text file? I suggest using a plain text file as Properties files are generally used for known property keys, whereas your program has no prior knowledge of what the team names will be.

    As to your question, are you asking how to read the information from the file back into your program? If so what you would do is read a line of text, split on the = and then you have the team name as the first element and the score as the second element. Remember that the score is still a String an if you need it as an int you will have to parse it.

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

    Default

    How would I write to the text file my keys and values?
    Also when I run it it gives me a NPE which is because I don't have initial values for my keys.
    How can I give them initial values?





    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at HouseHockey.Statistics.getbValue(Statistics.java:1 32)
    at HouseHockey.Statistics.<init>(Statistics.java:29)
    at HouseHockey.MainPage$2.actionPerformed(MainPage.ja va:89)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6374)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:613 9)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4736)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:45 66)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:45 66)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:680)
    at java.awt.EventQueue.access$000(EventQueue.java:86)
    at java.awt.EventQueue$1.run(EventQueue.java:639)
    at java.awt.EventQueue$1.run(EventQueue.java:637)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:653)
    at java.awt.EventQueue$2.run(EventQueue.java:651)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 650)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)
    Last edited by loopsnhoops; 06-01-2011 at 03:18 AM.

  15. #15
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    There are many sample programs on the forum that write text files. Do a search.

  16. #16
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at HouseHockey.Statistics.getbValue(Statistics.java:1 32)

    I'd start by looking at line 132 in your Statistics class.

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. Writing multiple lines to text file at once
    By Plex in forum New To Java
    Replies: 14
    Last Post: 11-08-2010, 09:17 PM
  3. Replies: 1
    Last Post: 02-27-2010, 01:29 AM
  4. writing to text file problem
    By blumdiggity in forum Networking
    Replies: 1
    Last Post: 02-26-2010, 02:43 PM
  5. writing text to file
    By notwist in forum New To Java
    Replies: 3
    Last Post: 04-25-2008, 04:20 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
  •