Home > Software engineering >  Multi dimensional array to JTable?
Multi dimensional array to JTable?

Time:05-26

How do I get my multi dimensional array to output all results from User class to Email_GUI class so I can input it as my data for JTable? Currently, it only display one result. Should I create a method using the logic in user class that runs trough the whole array [][]? if so, what would that look like?

NOTE: JTable only accept an array [][] as data input.

Email _GUI class:

 else if (e.getSource() == searchB) {
            searchB.setFocusable(false); // removes the border around create button
            firstName = fNameTF.getText().trim().toLowerCase();
            middleName = mNameTF.getText().trim().toLowerCase();
            lastName = lNameTF.getText().trim().toLowerCase();
            department = depTF.getText().trim().toLowerCase();
            userID = iDTF.getText().trim();
            userInformation = new User(firstName, middleName, lastName, department, passWord);
            //DefaultTableModel dTM = new DefaultTableModel(userInformation.pullUserInfo(userID),resultsColumnNames );
            resultTF = new JTable(userInformation.pullUserInfo(userID));
            resultTF.setAutoCreateRowSorter(true);
            resultTF.setShowGrid(false);
  

User Class:

 public class User {

    //Fields
    private String firstName;
    private String lastName;
    private String middleName;
    private String department;
    private String password;
    private int userID;
    private String[][] stringB;
    private  DefaultTableModel dTM;

// Method to search for user information 
    public DefaultTableModel pullUserInfo(String id) {
        String [] resultsColumnNames = {"User ID", "First Name", "Middle Name", "Last Name" , "Department", "Password", "Email"};
        String iD = id;
        String line;
        try {
            BufferedReader bufferRead = new BufferedReader(new FileReader("userFile.txt"));
            while ((line = bufferRead.readLine()) != null) {
                String[] rows = line.split(" ,");
                stringB = new String[rows.length][];

                // Return results for ID only if all remaining fields are blank
                if (line.startsWith(iD) && !iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {

                }
                // Return results for first name only and all the remaining fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {

                        stringB[r  ] = row.split(" ");
                        
                        dTM = new DefaultTableModel(stringB, resultsColumnNames );
                    }

                      

                }
                // Return results first name and middle name being entered and the rest of fields are emtpy
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {

                }
                // Return results first name, middle name, and last name, being entered and the rest of fields are emtpy
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && getDepartment().isBlank()) {

                }
                // Return results first name, middle name, last name, and department being entered an

Expected output via IDE:

[1, [william], [herbert], [jackson], [tcdl], [Password123456],[[email protected]]]

[[2], [andrew], [william], [jackson], [cdls], [ITdepartment1234], [[email protected]]]

[[4], [william], [], [jackson], [it], [1qazxsw2#EDCVFR$], [[email protected]]]

Actual Output: Screenshot of output

Whole User Class:

/* 
*File: Email_GUI.java
*Author: William
*Date: May 25, 2021
*Purpose: This program processes user information
 */

import java.text.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.concurrent.atomic.AtomicInteger;
import java.io.*;
import javax.swing.table.DefaultTableModel;

public class User {

    //Fields
    private String firstName;
    private String lastName;
    private String middleName;
    private String department;
    private String password;
    private int userID;
    private String[][] stringB;
    private DefaultTableModel dTM;

    //Constructor that processe user information
    public User(String fName, String mName, String lName, String depart, String pass) {
        this.firstName = fName;
        this.middleName = mName;
        this.lastName = lName;
        this.department = depart;
        this.password = pass;
    }

    public void setFirstName(String firstN) {
        this.firstName = firstN;

    }

    public void setMiddleName(String middleN) {
        this.middleName = middleN;

    }

    public void setLastName(String lastN) {
        this.lastName = lastN;

    }

    public void setDepartment(String dep) {
        this.department = dep;

    }

    public void setPassword(String pass) {
        this.password = pass;

    }

    public void setID(int iDent) {
        this.userID = iDent;
    }

    // get methods
    public String getFirstName() {
        return firstName;
    }

    public String getMiddleName() {
        return middleName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getDepartment() {
        return department;
    }

    public String getPassword() {
        return password;
    }

    public int getUserID() {
        return userID;
    }

    // method to generate email. This is being called in the StoreUserInfo() method
    public String userEmailGeneration(String firstName, String middleName, String lastName, String department, String userExist) {
        String email = null;
        Random emailNumberGenerator = new Random();
        int numberRandom = emailNumberGenerator.nextInt(500);

        switch (userExist) {
            case "false":
                if (middleName.isBlank()) {
                    email = firstName   "."   lastName   "@"   department   ".company.com";
                } else {
                    email = firstName   "."   middleName.charAt(0)   "."   lastName   "@"   department   ".company.com";
                }
                break;
            case "true":
                if (middleName.isBlank()) {
                    email = firstName   "."   lastName   numberRandom   "@"   department   ".company.com";
                } else {
                    email = firstName   "."   middleName.charAt(0)   "."   lastName   numberRandom   "@"   department   ".company.com";
                }

        }

        return email;

    }

    // Method to store the user's informatino like Firstname, LastName,etc...
    public void storeUserInfo() {
        AtomicInteger iDCount = new AtomicInteger(0);
        String userExist = "false";
        String userEmail = userEmailGeneration(getFirstName(), getMiddleName(), getLastName(), getDepartment(), userExist);
        try {
            FileWriter userFileWriter = new FileWriter("userFile.txt", true); // writes to the file and creates it if it does not exist
            Scanner scanFile = new Scanner(new File("userFile.txt"));
            while (scanFile.hasNext()) {
                iDCount = new AtomicInteger(scanFile.nextInt());
                if (scanFile.nextLine().contains(userEmail)) {
                    userExist = "true";
                }
            }
            scanFile.close();
            userID = iDCount.incrementAndGet();
            BufferedWriter bufferWriter = new BufferedWriter(userFileWriter); // Buffer to hold and write the user information
            bufferWriter.write(userID   " "   getFirstName()   " "   getMiddleName()   " "   getLastName()   " "   getDepartment()
                      " "   getPassword()   " "   userEmailGeneration(getFirstName(), getMiddleName(), getLastName(), getDepartment(), userExist));
            bufferWriter.newLine();
            bufferWriter.close();
        } catch (IOException e) {
            System.out.println("Error: has occured with file creation or location");

        }

    }

    // Method to search for user information 
    public DefaultTableModel pullUserInfo(String id) throws FileNotFoundException, IOException {
        String[] resultsColumnNames = {"User ID", "First Name", "Middle Name", "Last Name", "Department", "Password", "Email"};
        String iD = id;
        String line;
        try (BufferedReader bufferRead = new BufferedReader(new FileReader("userFile.txt"))) {
            while ((line = bufferRead.readLine()) != null) {
                String[] rows = line.split(" ,");
                stringB = new String[rows.length][];
                
                // Return results for ID only if all remaining fields are blank
                if (line.startsWith(iD) && !iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results for first name only and all the remaining fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                    
                }
                // Return results first name and middle name being entered and the rest of fields are emtpy
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                      
                    }
                }
                // Return results first name, middle name, and last name, being entered and the rest of fields are emtpy
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results first name, middle name, last name, and department being entered and the rest of fields are emtpy
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results middle name and the rest of fields are emtpy
                if (iD.isBlank() && getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results middle name and Last name while the rest of fields are emtpy
                if (iD.isBlank() && getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results middle name, Last name, and department while the rest of fields are emtpy
                if (iD.isBlank() && getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results  Last name while the rest of fields are emtpy
                if (iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results  Last name while the rest of fields are emtpy
                if (iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results  lastname, and first name while the rest of fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results  lastname, department, and first name while the rest of fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && getMiddleName().isBlank() && line.contains(getLastName()) && !getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results department, while the rest of fields are empty
                if (iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results department, and first name while the rest of fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results department, and middle name while the rest of fields are empty
                if (iD.isBlank() && getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    int r = 0;
                    for (String row : rows) {
                        stringB[r  ] = row.split(" ");
                        dTM = new DefaultTableModel(stringB, resultsColumnNames);
                    }
                }
                // Return results department,first name, and middle name while the rest of fields are empty
                if (iD.isBlank() && line.contains(getFirstName()) && !getFirstName().isBlank() && line.contains(getMiddleName()) && !getMiddleName().isBlank() && getLastName().isBlank() && line.contains(getDepartment()) && !getDepartment().isBlank()) {
                    
                }
            }
        }

        return dTM;

    }

    //create two dimension array method that takes in the LINE data.
    // create anoter medthod that simply spits out the two dimensional array to the TF of the main class
}

CodePudding user response:

This is the general framework. You can add in your missing values policy:

    // Method to search for user information
public static DefaultTableModel pullUserInfo(String id) throws FileNotFoundException, IOException {
    String[] resultsColumnNames = { "User ID", "First Name", "Middle Name", "Last Name", "Department", "Password",
            "Email" };
    DefaultTableModel result = new DefaultTableModel(0, resultsColumnNames.length);
    String line = null;
    try (BufferedReader bufferRead = new BufferedReader(new FileReader("userFile.txt"))) {
        while ((line = bufferRead.readLine()) != null) {
            String[] row = line.split("\\s*,\\s*");
            result.addRow(row);
        }
    }
    result.setColumnIdentifiers(resultsColumnNames);

    return result;

}

CodePudding user response:

I figured it out.I just added the bufferread.realine() to my row of my defaultTableModel. I pays to take a break and then revisit the code when all rested:

// Method to search for user information 
    public DefaultTableModel pullUserInfo(String id) throws FileNotFoundException, IOException {
        // Column Names for the Default Table model that is pushed to JTable in Email_GUI class
        String[] resultsColumnNames = {"User ID", "First Name", "Middle Name", "Last Name", "Department", "Password", "Email"};
        dTM = new DefaultTableModel(0, resultsColumnNames.length);
        dTM.setColumnIdentifiers(resultsColumnNames);
        String iD = id;
        String line;
        try ( BufferedReader bufferRead = new BufferedReader(new FileReader("userFile.txt"))) {
            while ((line = bufferRead.readLine()) != null) {

                // Return results for ID only if all remaining fields are blank
                if (line.startsWith(iD) && !iD.isBlank() && getFirstName().isBlank() && getMiddleName().isBlank() && getLastName().isBlank() && getDepartment().isBlank()) {
                    dTM.addRow(line.split(" "));
                }

return dTM;
  • Related