![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Software Developer
|
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)
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)
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)
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?
__________________
I'm trying to change the world, but I can't find the source code... |
|
|
|
|
|
#2 |
|
Hobbyist Programmer
|
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?
|
|
|
|
|
|
#3 |
|
Software Developer
|
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... |
|
|
|
|
|
#4 |
|
Hobbyist Programmer
|
Re: Problem Reading a File
Sorry, my bad, try to replace Object with String in addNationality method.
|
|
|
|
|
|
#5 |
|
Software Developer
|
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... |
|
|
|
|
|
#6 |
|
Hobbyist Programmer
|
Re: Problem Reading a File
|
|
|
|
|
|
#7 |
|
Software Developer
|
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... |
|
|
|
|
|
#8 |
|
Professional Programmer
Join Date: Jul 2011
Posts: 430
Rep Power: 2
![]() |
Re: Problem Reading a File
Change this:
java Syntax (Toggle Plain Text)
to this: java Syntax (Toggle Plain Text)
|
|
|
|
|
|
#9 |
|
Software Developer
|
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... |
|
|
|
|
|
#10 | |
|
Professional Programmer
Join Date: Jul 2011
Posts: 430
Rep Power: 2
![]() |
Re: Problem Reading a File
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:
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 |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
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 |