简体   繁体   中英

Storing 1D arrays inside 2D array from text file to jTable java

Hi I need to import a text file into a Jtable. I have to store each column into a separate array so I was thinking to create a 2D array for the table, and 1D array for each column inside. (9 columns, 20 rows total)

All i've managed to do so far is import the text file and display it in the Jtable. The data types i have to use are int, string and double(or float) as I have to create as student table where their ID numbers (int )names, address ect (string) and grades (double) will be shown and ill have to be able to do calculations with the grades.

 private void formWindowOpened(java.awt.event.WindowEvent evt) {                                  
    // When the window is opened, the information from the text file will be loaded into the table (tblStudentDetails)


    try {
        BufferedReader br = new BufferedReader(new FileReader ("C:\\Users\\XXX\\NetBeansProjects\\StudentDetailsJava\\lib\\StudentDetails.txt"));

        //Get the first line
        //Get the columns name from the first line
        // Set columns name to the tblStudentDetails model 

        String firstLine = br.readLine().trim(); // Reading the file line (row) of the text file
      //  while(firstLine !=null) {
        String[] columnsName = firstLine.split("\t"); // The coloums are split by tab

        DefaultTableModel model = (DefaultTableModel)tblStudentDetails.getModel();
        model.setColumnIdentifiers(columnsName);

        // Get lines from txt file
        Object[] tableLines = br.lines().toArray();


              // Extract data from lines
              // Set data to tblStudentsData model
              for (Object tableLine : tableLines) {
                  String line = tableLine.toString().trim();
                  String[] dataRow = line.split("\t");
              {

              model.addRow(dataRow);   
              }
              }

                   br.close();
    } catch (Exception ex) {
        Logger.getLogger(StudentTable.class.getName()).log(Level.SEVERE, null, ex);
    }


}   

Here's what I have so far, and before you all kill me, i've spent the past 3 days trying to get this to work and i've tried nearly every solution I can find on this site to no avail.

I've been able to create a 2D array but I haven't been able to actually make it work when reading the text file, or importing it to the Jtable. Someone help! first year student so I'm definitely a newbie and my assignment is due soon, I cant move on without solving this!

thanks! - Mrs x

Also The text file looks like this (separated by tab so the first row is too long, but it imports into the Jtable just fine)

enter image description here

try this code:

Scanner input = new Scanner(new File(FILE_NAME));
    int rows = 0;
    String[] columnsName = null;
    if (input.hasNext()) {
        columnsName = input.nextLine().split("\t");
    } else {
        return;
    }

    while (input.hasNextLine()) {
        input.nextLine();
        ++rows;
    }
    String[][] a = new String[rows][columnsName.length];

    input.close();

    input = new Scanner(new File(FILE_NAME));
    if (input.hasNextLine()) {
        input.nextLine();
    }

    for (int i = 0; i < rows; i++) {
        a[i] = input.nextLine().split("\t");
    }

    JTable jt = new JTable(a, columnsName);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM