• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

writing enum types to a text file?

 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is this possible? I'm learning java swing atm, the program I'm working on allows the user to input artist name, song name etc and then I have an enum class where by the user just inputs a char value, so for example d would be DEEPHOUSE , t for TECHNO and I want it to appear as TECHNO or deephouse in the text file but it only prints d or t



In the text file it appears as

kerri chandler chaos ep king street 1 9.99 d



but I want it to appear as

kerri chandler chaos ep king street 1 9.99 DEEPHOUSE




thanks for reading.
 
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please show how you are writing the text file. Also the toString() method in your enum.
You realise you can use the valueOf() method which all enums have; more details in the Java® Language Specification (=JLS). You would need the user to enter the full name of each object, and you may need to use toUpperCase() on the input.

[addition] Get your application working correctly before you try writing a GUI.
 
Kevin O'Sullivan
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This method does the writing to text file.



I don't have a tostring written as I didn't think I needed it.

 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't want a new JFrame.
If you are writing the contents of a text box, and that only contains one letter, that is what you will write. You want to write the enum element, which will default to TECHNO, etc., if you use the standard version of toString().
 
Kevin O'Sullivan
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



I changed t6.gettext() to addGenre and now It works the way I want it too, Is this a good approach?

When working with text files, Is it possible to add a header to the top of the page? so for example Artist, songname etc then to have the all the data below it ?
 
Rancher
Posts: 162
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sure you can. A file contains what one writes into it. If you write executable binary data into it you get an executable. If you want a header just write it before you write your data.
 
Kevin O'Sullivan
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a particular way of doing it so everything stay in line? I tried adding the headers to the top of txt file by just typing them but they wouldnt stay In line with one another .
 
Marshal
Posts: 22453
121
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What do you mean with "stay in line"?

You're using tab characters as separators. If your values have different lengths, these tabs are located in different columns. That does lead to text that isn't aligned. However, that shouldn't matter; comma or tab separated files are not really meant to be read by people. You can easily import them in applications like Excel which does provide a nice table layout.
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:. . .  Is this a good approach? . . .

Probably not. I am worried about seeing a method called addGenre() which returns the name of the genre. I think you have been programming from the GUI to the application when you should usually program from the application to the GUI.
 
Kevin O'Sullivan
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kevin O'Sullivan wrote:. . .  Is this a good approach? . . .

Probably not. I am worried about seeing a method called addGenre() which returns the name of the genre. I think you have been programming from the GUI to the application when you should usually program from the application to the GUI.



Can you elaborate why you're worried about it ?
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An addXXX() method usually returns boolean to signify whether the addition was actually executed, or it is void. If you call it repeatedly, what is it doing? Is it adding the genre several times? Is it adding a genre at all, or simply the contents of a text box?
 
Kevin O'Sullivan
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:An addXXX() method usually returns boolean to signify whether the addition was actually executed, or it is void. If you call it repeatedly, what is it doing? Is it adding the genre several times? Is it adding a genre at all, or simply the contents of a text box?



It returns the enum type depending on which char value is entered,How would you go about writing it?
 
Bartender
Posts: 4633
182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would put the addGenre method in the Genre enum.
Make a static Map<Character, Genre> in Genre, and initialize it.
Add a static method

and then you can find your enum by using
 
Campbell Ritchie
Marshal
Posts: 74048
332
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kevin O'Sullivan wrote:. . . It returns the enum type depending on which char value is entered,How would you go about writing it?

You mean the enum constant is returned. The method is obviously called on the same object as we are in, and it doesn't take a parameter, so it is not at all clear how it works.
You don't write chars in a text component; you write Strings. If you need a char, you have to use charAt(0) or similar.
I probably wouldn't write such a method at all. I would have a book object with a genre field, and use its getGenre() method. Then you can use toString(). If you write x + y where x is a String and y is a non‑null reference to an object, the + operator causes toString() to be called.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic