• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reading from a file

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to read contacts in from a file (each contact has 4 lines in the file - name, address etc) and then display them in a JTable.
I've created a JTable, but am currently using test data because I can't read it in from the file.
This is my test data:

I'm not sure if I'm on the right track, but I think I need to read in 4 lines from the file and then surround them by {} and keep repeating the process until I get to the end of the file. Is that right?
Can I do this by creating an ArrayList and a substring for each contact? or am i completely on the wrong track?
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, but you would do better (I am sure I have told you before) to create an Address class or NameAndAddress which encapsulates those Strings into one object.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

and then surround them by {}


Something like that would work. Not very well, but it would work.

The problem with using array-of-arrays is that you only know the size of the '-of-arrays', I imagine? You would have to read the whole file first to 'know' how many address entries there were? What you want to do is build up a list of arrays of a known size, so use a LinkedList of String arrays:



woah, I'm dying of attempts to use TAB and vi editing commands. I must learn to use these new fangled 'windows' one day!
My code might not be spot on, just typed it from memory, but should give you plenty of pointers to get you started with reading from a file into a data structure. Campbell Ritchie is right - you should do it like he says. There are loads of pitfalls to doing it this way, but it will at least wean you off hard-coded data!
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your help Sean.
Having a few problems though...


The code I've got isn't putting the contacts into the JTable. I've been using println after each line to see where it's been going wrong and I think it's at line 23. It's reading in each line correctly, but I don't think it's putting them into the Array. When I try to println the addressArray I'm getting this:
[Ljava.lang.String;@a59698
[Ljava.lang.String;@141d683
[Ljava.lang.String;@16a55fa
[Ljava.lang.String;@32c41a
[Ljava.lang.String;@e89b94

Any idea where i'm going wrong?
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you're assuming that line 26 will put the data into field "addresses" - not so. The call returns the array, so you probably want to do something like:

addresses = listAddress.toArray(addresses)
 
charlie mills
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Edit: I just understood what you meant.
Yeah, that is what I needed to do, thank you!
 
Sean Collins
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

The call returns the array


That call: LinkedList.toArray() returns the array you provided as an argument, IF it's big enough to hold all the items in the LinkedList, a new array if the passed-in array is not big enough. It might be my sloppy coding style that has confused matters, but it would work.

Charlie's problem is that he initialised the array (line 8) before he filled the list. He initialised the list in line 7, so the call to LinkedList.size() in the line after would return 0.

When you print the array, Charlie, you're getting the 'right' output - for Java! The left-square bracket means 'array', the L means something which I can't remember, but which might be 'reference type' (as opposed to built-in like 'int' or 'char' - see the Language Specification, I think...), the java.lang.String is the class of the reference type, and the @hex is either a 'memory' (or whatever passes for memory in a VM) or a hashcode for the object. There's some really essential Java info contained in that output! When you get your head around exactly what is being printed by Object.toString(), you're really on your way to fully understanding Java. There is a method in java.util.Arrays - deepToString() - which might give you something more like what you're expecting.

What your output is telling me is that you have initialised 5 references to String arrays. The code that follows, where you use 'toArray' with a zero-length array-of-arrays, creates a NEW array-of-arrays (because it can't fit the list in a zero-length array), and returns it on the unassigned Left-Hand-Side (LHS), where it just disappears! You could either try assigning it to your 'addresses' array-of-arrays at that point, or move the creation of the empty array to after the loop, where the final size of your LinkedList is known.
 
I yam what I yam and that's all that I yam - the great philosopher Popeye. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic