Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Jun 28th, 2012, 12:34 PM   #1
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 7 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Problem Reading a File

I'm writing a program which will calculate the percentage of each nationality you are based on the family tree you specify. I'm running into problems when reading in the list of nationalities from a file. I don't know if maybe I just put the file in the wrong folder, or if it's the way I read the file / the data type I'm using to store it, but when I pull up the dialog box that is supposed to show a drop down list with all the nationalities, it gives me what looks like an error as my only choice in the drop down.

Here is the function I use to read in the data:
java Syntax (Toggle Plain Text)
  1. public Set<String> populateNationalities() throws FileNotFoundException {
  2. File inFile = new File("demonyms.txt");
  3. String line = "";
  4. Set<String> demonyms = new HashSet<>();
  5.  
  6. BufferedReader reader = new BufferedReader(new FileReader(inFile));
  7.  
  8. try {
  9. while((line = reader.readLine()) != null) {
  10. demonyms.add(line);
  11. }
  12.  
  13. reader.close();
  14. } catch(IOException io) {
  15. System.err.println(io);
  16. System.exit(1);
  17. }
  18.  
  19. return demonyms;
  20. }

Now, here is the code snippet I used in the constructor to assign those values to the HashSet defined in the class:
java Syntax (Toggle Plain Text)
  1. try {
  2. nationalities = populateNationalities();
  3. } catch (FileNotFoundException ex) {
  4. System.err.println(ex);
  5. System.exit(1);
  6. }

And finally, here is the function I use to add a nationality... it adds it to another HashSet which is in a class called Person, which is the class that the Tree is made of.
java Syntax (Toggle Plain Text)
  1. public void addNationality() {
  2. if(nationalities != null) {
  3. Object[] demonyms = {nationalities.toArray()};
  4. String nationalityChoice = (String) JOptionPane.showInputDialog(this, "Choose a nationality.", "Nationalities", JOptionPane.INFORMATION_MESSAGE, null, demonyms, demonyms[0]);
  5. familyTree.getCurrent().getData().addNationality(nationalityChoice);
  6. updateLists();
  7. } else {
  8. System.err.println("Nationalities list could not be loaded.");
  9. System.exit(1);
  10. }
  11. }

When I update the list in the updateLists() function, I'm just retrieving the Person object from the current node in the tree and passing its nationality list's .toArray() value to the JList's setListData() method. So why is it giving me this error? (screenshots attached)

I probably just forgot to convert something somewhere, or maybe the file is in the wrong place? I put it in the folder with the compiled .class file (MainFrame.class), so I don't think that's it.

Anyone see something I'm not?
Attached Images
File Type: png heritage_error.png (49.0 KB, 3 views)
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Jun 28th, 2012, 1:32 PM   #2
Sinus
Hobbyist Programmer
 
Join Date: Jul 2011
Location: Lithuania
Posts: 120
Rep Power: 4 Sinus is on a distinguished road
Send a message via Skype™ to Sinus
It's not an error. It just shows your Object as string. What about instead putting Object demonyms in JOptionPane, you'd put JComboBox which contains strings from demonyms?
Sinus is offline   Reply With Quote
Old Jun 28th, 2012, 2:09 PM   #3
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 7 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Problem Reading a File

I'm not exactly sure what you mean. What does a JComboBox have to do with my input dialog? It also shows these strings in my "Nationalities" JList if I navigate through the tree.
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Jun 28th, 2012, 2:45 PM   #4
Sinus
Hobbyist Programmer
 
Join Date: Jul 2011
Location: Lithuania
Posts: 120
Rep Power: 4 Sinus is on a distinguished road
Send a message via Skype™ to Sinus
Re: Problem Reading a File

Sorry, my bad, try to replace Object with String in addNationality method.
Sinus is offline   Reply With Quote
Old Jun 28th, 2012, 3:06 PM   #5
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 7 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Problem Reading a File

The .toArray() method of the HashSet converts it to Object[]. I can't use String[]...
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Jun 28th, 2012, 3:56 PM   #6
Sinus
Hobbyist Programmer
 
Join Date: Jul 2011
Location: Lithuania
Posts: 120
Rep Power: 4 Sinus is on a distinguished road
Send a message via Skype™ to Sinus
Re: Problem Reading a File

Have you tried debugging?

It is kinda strange, I've tried to run something similar on my JVM and it worked (or maybe missed something):

Sinus is offline   Reply With Quote
Old Jun 28th, 2012, 9:34 PM   #7
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 7 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Problem Reading a File

The only thing I could think of is maybe it's not reading the information from the file properly.
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Jun 28th, 2012, 11:29 PM   #8
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Problem Reading a File

Change this:
java Syntax (Toggle Plain Text)
  1. Object[] demonyms = {nationalities.toArray()};

to this:
java Syntax (Toggle Plain Text)
  1. String[] demonyms = nationalities.toArray(new String[]{});
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Old Jun 29th, 2012, 12:38 AM   #9
PaCkEtPiRaTe
Software Developer
 
PaCkEtPiRaTe's Avatar
 
Join Date: Aug 2007
Location: Massachusetts, USA
Posts: 696
Rep Power: 7 PaCkEtPiRaTe will become famous soon enough
Send a message via AIM to PaCkEtPiRaTe Send a message via Skype™ to PaCkEtPiRaTe
Re: Problem Reading a File

Well, I'm not really sure how it worked, but it did. I didn't see any versions of that method that took any arguments... what did it do differently? It fixed the input dialog, but the JList that shows each ancestor's nationalities is still showing those funny object strings.
__________________
I'm trying to change the world, but I can't find the source code...
PaCkEtPiRaTe is offline   Reply With Quote
Old Jun 29th, 2012, 1:36 AM   #10
macteki
Professional Programmer
 
Join Date: Jul 2011
Posts: 437
Rep Power: 4 macteki will become famous soon enough
Re: Problem Reading a File

Quote:
Originally Posted by PaCkEtPiRaTe View Post
Well, I'm not really sure how it worked, but it did.
It worked because showInputDialog() is expecting String array instead of Object array. If you passed an object array into it, the default "toString()" method would be called, which is implementation dependent.

Quote:
Originally Posted by PaCkEtPiRaTe View Post
I didn't see any versions of that method that took any arguments... what did it do differently?
There are two method declaration for toArray() in java.util.Set

1. Object[] toArray()
Returns an array containing all of the elements in this set.

2. <T> T[] toArray(T[] a)
Returns an array containing all of the elements in this set; the runtime type of the returned array is that of the specified array.

See this link for reference.

http://docs.oracle.com/javase/6/docs.../util/Set.html
__________________
API belongs to public. Knowledge belongs to yourself

http://java.macteki.com
macteki is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem in reading image file ramhanuman Java 0 Apr 3rd, 2011 1:52 PM
DOS batch file to FTP download files by reading from the text file. Abhijat_PF Other Programming Languages 1 Sep 14th, 2010 2:48 PM
reading problem from file called "INPUT" singh C 2 Jan 30th, 2010 7:31 AM
c++ reading from a file ... programmingnoob C++ 8 Apr 22nd, 2006 6:17 PM
reading in a file in java ryanl Java 3 Sep 8th, 2005 9:54 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:12 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC