Java 如何在将2D数组写入CSV文件并显示在表格上时,消除其空值/元素?

Java 如何在将2D数组写入CSV文件并显示在表格上时,消除其空值/元素?,java,arrays,csv,user-interface,null,Java,Arrays,Csv,User Interface,Null,我的Java代码有一个问题,它一直在我的GUI上显示空值,特别是从它的2D数组(String[][]cust=new String[10][7]——这是一个通用的2D数组),当我将这些值写入CSV文件并在我的GUI中的表上显示时,如何去除这些值,它还完全迭代数组的整个大小(即10)在表中显示了所有的空值。我在for循环中做了一个if语句,以消除cust[r][0]!=null的空值,但它仍然不能很好地工作 以下是我编写和读取CSV文件并将其显示在表格上的源代码: public void

我的Java代码有一个问题,它一直在我的GUI上显示空值,特别是从它的2D数组(String[][]cust=new String[10][7]——这是一个通用的2D数组),当我将这些值写入CSV文件并在我的GUI中的表上显示时,如何去除这些值,它还完全迭代数组的整个大小(即10)在表中显示了所有的空值。我在for循环中做了一个if语句,以消除cust[r][0]!=null的空值,但它仍然不能很好地工作

以下是我编写和读取CSV文件并将其显示在表格上的源代码:

  public void writeCustomerCSV(){  // everything in this snippet code works fine(it creates a CSV file which stores the inputs of the user)
   try{
       BufferedWriter bw = new BufferedWriter(new FileWriter("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
       StringBuilder sb = new StringBuilder();
        
       int y;
       for(int x = 0; x < itemTo2D.length; x++){
           for(y = 0; y < itemTo2D[0].length; y++){
               if(itemTo2D[x] != null){
                   sb.append(itemTo2D[x][y]);
                 
               }
                 sb.append(",");
           }
            
           sb.append("-");  //separation for rows
           sb.append(",");  // separation for columns
            
       }
        
       bw.write(sb.toString());
       bw.close();
        
   }  catch (Exception ex){
        
     }
   
  }



public void readCustomerCSV(){  // reads the contents of the CSV file    
    int read2DStringIndex = 0;
    int newVarIndexer = 0;
    String[] fromfile = {};   // 1d string for getting the columns(7 columns) of the CSV file

 

    try{
         BufferedReader br = new  BufferedReader(new FileReader("C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.csv"));
         String line;
         
         while ((line = br.readLine()) != null){
             fromfile = line.split(",");  //separates the columns by a comma
         }
        
        
        
    } catch (Exception ex){
        
    }
    
    for(int g = 0; g < fromfile.length; g++){  
         if(fromfile[g].equals("-")){   //if there is a presence of a dash, it increments the read2DStringINdex (row index) of the 2D array     
             read2DStringIndex++;
              newVarIndexer = 0;

         }
         else{
               cust[read2DStringIndex][newVarIndexer] = fromfile[g];    //cust is the 2D array(declared universal) which is going to display the values to the table
               newVarIndexer++;

         }
    }
    

    DefaultTableModel tblmodelll = (DefaultTableModel) mainTable.getModel();  // table
    setrowcount = 0;

    for(int r = 0; r < cust.length; r++){
        if(setrowcount == 0){
           tblmodelll.setRowCount(0);
        }
        if(cust[r][0] != null){  //gets rid of the null but it is not working 
           tblmodelll.addRow(cust[r]);  //displays the cust(2D array) data to table 
         }
    
           setrowcount++; 
   }


    
    
    for(int h = 0; h < cust.length; h++){  //prints cust (2D array) , just to check what data is being stored
      for(int p = 0; p < cust[0].length; p++){
          System.out.println(cust[h][p] + ",");
       }
   }

    
public void writeCustomerCSV(){//此代码段中的所有内容都可以正常工作(它创建了一个CSV文件,用于存储用户的输入)
试一试{
BufferedWriter bw=新的BufferedWriter(新文件编写器(“C:\\Users\\RALPH\\Documents\\Database Java CSV\\customers.CSV”);
StringBuilder sb=新的StringBuilder();
int-y;
对于(int x=0;x
您可以使用commons-lang3包将null转换为blank.Maven依赖项,如下所示
org.apache.commons-lang3.12.0
并使用:StringUtils.defaultString(yourString);这会将null转换为问题代码中的balnk:
if(cust[r][0]!=null){//去掉空值,但它不起作用
,这是因为您需要检查字符串“null”,如:
if(cust[r][0].equals(“null”){
,因为您的CSV文件不包含实际的空值,所以它包含单词“null”.@ankurpramanik就是这个吗?@Abra好的,你给出的语句起作用了!!我只是对它做了一点修改,因为它导致了空指针异常。这里是:试试{if(cust[r][0]。equals(“null”)==false){//去掉空值,但它不起作用tblmodell.addRow(cust[r]);//显示cust(2D数组)表}catch的数据(异常e){}@Ralph Henry是的,您的链接是正确的