Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Array

  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Array

    Hi, i've got a text file that im reading in and i want to put it into a jTable, i have to store the contents from the text file into an array first i think any one know how to?

    Ill put my code here if it helps .

    package cwhints;

    import java.io.*;

    // reading text from a file
    public class ReadingFile {

    public static void main(String[] args)
    throws IOException {


    BufferedReader br = new BufferedReader(
    new FileReader("file1.txt"));



    String s;
    String name = "";
    String number = "";
    String area = "";

    while ((s = br.readLine()) != null) {
    if (s.equals("<stock_enquiry>")) {
    name = br.readLine();
    phone = br.readLine();
    branch = br.readLine();
    System.out.println("name is " + name + " is " + number + " is " + area);
    }
    }
    br.close();
    }}
    }

    Thanks

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    jTables require multi-dimensional arrays, vectors, or explicit setting to table cells in order to display data.
    You'll probably want to change the way you're data is stored in the file though, especially if you need to deal with possibly empty data. In any case, you'll want to combine each set of user, number and area together into a single record.
    Code:
    String[] aColumns = {"Name", "Phone", "Area"};
    Object[][] aRecords = {
        {"First", 1234, "Area"},
        {"Second", 1234, "Area"}
    };
    JTables also allow the use of Vectors, which is generally a better option for a dynamic number of records. I'm *pretty* sure its format is Vector for column names, and Vector of Vectors for records within the table.
    This would look something more along the lines:
    Code:
    Vector<String> vColumns = new Vector<String>(3);
    Vector<Vector> vRecords = new Vector<Vector>();
    vColumns.add("Name");
    vColumns.add("Phone");
    vColumns.add("Area");
    
    // Then, with you're retrieved information:
    Vector vTemp = new Vector(3);
    vTemp.add(name);
    vTemp.add(phone);
    vTemp.add(area);
    vRecords.add(vTemp);
    
    JTable myTable = new JTable(vRecords, vColumns);
    Vectors are also serializable, so you can use this to you're advantage. I would extend the Vector class to handle you're own information (ie, create a collection of you're name, phone and area) and use this to write it to a file.

    Here is a link to JTable examples, its pretty good: http://java.sun.com/docs/books/tutor...nts/table.html
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Isn't there a loop that will store the data in an array from the file?

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    There is not built in method (to my knowledge) using scanner and file that allow you to read the entire file into an array. You simply read it line by line until you've reached EOF.
    But an array is not what you want. Arrays are non-scalable so in order to increase you're size you'd need to write a method that takes you're array, creates a new array of size passed array + 1, copies all the values from the old array into the new array and returns it. A vector is exactly like an array but it does this for you when you use .add, and also the reason so many objects accept a vector instead of an array. I'm about 85% certain that Vector also has a toArray method on it.

    The other option is serializable. Consider a collection of say AddressBook, where each record is a name, number and area (can be an array or vector or any collection type). If AddressBook is of type serializable, you can write the entire object to a file. When you read the file back in, it can be casted into a type of AddressBook providing you with the original data written to the file. Super easy. Downside is, you cannot write primitive data for file uploading.

    I do not like filehandling in Java. C is much easier to use for file writing.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •