• Post Reply Bookmark Topic Watch Topic
  • New Topic

using a text file to get variables for a gui  RSS feed

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All!!

I have a question regarding getting variables for a program i am writing with a text File. I have a tabbed pane and in one tab i have a JComboBox with filter numbers using a simple array , when i pick a filter in the comboBox i then have to get a filter number from a text file. The filter numbers should be able to be changed on another tab with JTextField, then using a button once the user has typed in the new number to save that to the text file in place of one that was there.
the Filter number format is "RI1234567" for instance. I would like to know which methods i would need to use to accomplish this. I haven't done much at all in the way of IO so your help would be greatly appreciated!!

i currently have the filter numbers saved in an array --> private String[] currentFilterNumbers = {"IR4658392","IS3509856","IP1234567","IR3456987"}; but i can't change them like this, so should i get this directly from the text file? or should i have a list that is updated from the text file?

I hope i have explained my problem well enough, if not please ask and i will try to explain it better.

Thanks
Mike
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:Hi All!!

I have a question regarding getting variables for a program i am writing with a text File. I have a tabbed pane and in one tab i have a JComboBox with filter numbers using a simple array , when i pick a filter in the comboBox i then have to get a filter number from a text file. The filter numbers should be able to be changed on another tab with JTextField, then using a button once the user has typed in the new number to save that to the text file in place of one that was there.
the Filter number format is "RI1234567" for instance. I would like to know which methods i would need to use to accomplish this. I haven't done much at all in the way of IO so your help would be greatly appreciated!!


It's not clear what part you're having trouble with. If it's how to read the data from the file, start by googling for java IO tutorial. If it's something else, can you please clarify?

i currently have the filter numbers saved in an array --> private String[] currentFilterNumbers = {"IR4658392","IS3509856","IP1234567","IR3456987"}; but i can't change them like this


Sure you can.


The only thing you can't do is change the size of the existing array.

, so should i get this directly from the text file? or should i have a list that is updated from the text file?


Not sure what you mean by these two options.

 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Thanks for your reply! I can save the currentFilterNumber[0] of course while the program is running but it needs to be saved when the porogram is closed and kept in a text file. So my question regarding getting the variables from a text file directly or having a list be updated from the text file i meant can i just save these variables only in a text file and have them called when the program runs, or do i even need a list or array to keep them as well? I suppose i would use a list to get the variables from that text file correct?
I will take a look at that tutorial you mentioned, i seem to have a slight problem understanding Filewriter and BufferedWriter and so on...

Thanks
Mike
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:Hi Jeff,

Thanks for your reply! I can save the currentFilterNumber[0] of course while the program is running but it needs to be saved when the porogram is closed and kept in a text file. So my question regarding getting the variables from a text file directly or having a list be updated from the text file i meant can i just save these variables only in a text file and have them called when the program runs, or do i even need a list or array to keep them as well? I suppose i would use a list to get the variables from that text file correct?


That depends on your requirements/use case. If you just need the values once at startup, and the code to just use them immediately upon reading them is clean and easy to understand, then sure, it's fine to do it that way. Just don't jump through hoops to force it into that situation because you think you're saving something by not storing them in variables or an array or collection.
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,


Well i need to have them there on start up, and then need to be able to change one which will happen once every six months and sometimes in between. I would actually like to use an array as this is a small amount of data anyway, i am currently trying to write to the file using this method


I am having trouble at the moment with NumberFormatException as there are numbers and letters in the array while using the putInt(Integer.ParseInt(aktuelleNummer)), so am trying to work around it but no luck so far...not sure how to get around that problem.


Thanks
Mike
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mike ryan wrote:
I am having trouble at the moment with NumberFormatException as there are numbers and letters in the array while using the putInt(Integer.ParseInt(aktuelleNummer)), so am trying to work around it but no luck so far...not sure how to get around that problem.


At the risk of stating the obvious, you can't parse letters into an int. Not in base-10 anyway. So you have 3 choices that I can see:

1) Get rid of the letters.

2) Put the letters and numbers in a known order in the file, so as you're reading it, you know whether or not you're currently reading a number that can be parsed with parseInt().

3) Introduce some structure/metadata into the file, so that at each step you can tell whether you're about to read a letter or number. This could be anything from a simple leading "I:" for ints and "S:" for Strings to using key/value pairs (java.util.Properties) to letting Java do the serialization for you, by wrapping these things up in a class and making it Serializable (binary format) or using java.beans.XMLEncoder (text format, somewhat human-readable).
 
mike ryan
Ranch Hand
Posts: 210
Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,

Thanks for the help, sounds allot more difficult than i had thought! I will read up on key/value pairs (java.util.Properties) and see if i can do it that way. This is all new ground for me so will have to try it and see what i get.

Thanks
Mike
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!