Java Excel搜索和ArrayList未正常工作。

huangapple go评论73阅读模式
英文:

Java Excel search and Array Lists not working correctly

问题

I have trouble with my code. It does not work the way I wanted it to work. Sorry if I explain myself so badly. Java Excel搜索和ArrayList未正常工作。

(I use Apache POI version 4.1.1)

My problem: I have an excel-sheet with multiple entries. Each entry contains, among other things, the shipping date in column 1, the customer name in column 2, the pallet ID in column 15, and the shipping status in column 16. On top of everything, there is a headline for the columns.

The excel contains multiple customers and some of them multiple times, the only difference are the pallet IDs. My customer object class contains, among other things, an array list, which may contain multiple, different, pallet IDs.

I want to list each customer, whose shipping is today, only once, but with all his pallet IDs.

My customer class looks like this:

import java.util.ArrayList;
import java.util.Date;

public class Customer {
    
    private String customername;
    private ArrayList<String> pallet = new ArrayList<>();
    private String date;
    private String status;
    
    public Customer(){
        
    }

    public String getCustomerName() {
        return customername;
    }

    public void setCustomerName(String customername) {
        this.customername = customername;
    }

    public String getFromPallet(int i) {
        return pallet.get(i);
    }
    
    public ArrayList<String> getPallet() {
        return pallet;
    }
    
    public ArrayList<String> get(int j) {
        return pallet;
    }

    public void setPallet(ArrayList<String> pallet) {
        this.pallet = pallet;
    }
    
    public void addPallet(String string) {
        this.pallet.add(string);
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
    
    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String toString(){
        return String.format("%1s - %2s - %3s", customername, date, status);
    }    
    
}

my main class looks like this:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewTrueSimpleExcelReader {
    
    SimpleDateFormat SDF = new SimpleDateFormat("dd.MM.yyyy");
    
    Date today = new Date();
    Date BBB;
    int lastfound = 0;
    
    public List<Customer> readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
        
        List<Customer> listCustomers = new ArrayList<>();
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheetAt(0);
        
        XSSFRow firstrow = sheet.getRow(1);
        
        int fr = firstrow.getRowNum();
        XSSFRow row;
        
        while(fr < sheet.getLastRowNum()){
            
            row = sheet.getRow(fr);
            if(!row.getCell(0).getStringCellValue().equals("")){
                String AAA = row.getCell(0).getStringCellValue().trim();
                BBB = SDF.parse(AAA);
            
                if((SDF.format(BBB)).equals(SDF.format(today))){
                    Customer aCustomer = new Customer();
                    
                    System.out.println("Customer: "+row.getCell(1).getStringCellValue().trim());
                    
                    if(listCustomers.size() > 0 && row.getCell(16) == null){
                        
                        for(int i=0; i<listCustomers.size(); i++){
                            
                            System.out.println("i is: "+i);
                            String string1 = listCustomers.get(i).getCustomerName();
                            String string2 = row.getCell(1).getStringCellValue();
                            
                            if(string1.equals(string2) && row.getRowNum() != lastfound){
                                lastfound = row.getRowNum();
                                System.out.println("Dupl found");
                                for(int j=0; j<listCustomers.get(i).get(j).size(); j++){
                                    if(!row.getCell(15).getStringCellValue().trim().equals(listCustomers.get(i).getFromPallet(j))){
                                        listCustomers.get(i).addPallet(row.getCell(15).getStringCellValue());
                                    }
                                    
                                }
                            } else{
                                if(row.getRowNum() != lastfound){
                                System.out.println("second+: "+row.getCell(0).getStringCellValue().trim());
                                lastfound = row.getRowNum();
                                aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                                aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                                aCustomer.setStatus("unused");
                                if(row.getCell(15) != null){ 
                                aCustomer.addPallet(row.getCell(15).getStringCellValue());
                                }
                                listCustomers.add(aCustomer);
                            }
                            }
                        }
                        
                    } else{ 
                        if(row.getCell(16) == null){
                        lastfound = row.getRowNum();
                        System.out.println("first: "+row.getCell(0).getStringCellValue().trim());
                        aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
                        aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
                        aCustomer.setStatus("unused");
                        if(row.getCell(15) != null){
                        aCustomer.addPallet(row.getCell(15).getStringCellValue());
                        }
                        listCustomers.add(aCustomer);
                    }
                    }
                    
                    
                    
                }
            
            }
           
          fr++;  
        }
        
        
        System.out.println(listCustomers.size());
        wb.close();
        inputStream.close();
        return listCustomers;
    }
    
}

Some customers are listed multiple times with the same name on the same date, but with different pallet IDs, so my code should just add the different IDs to already existing customers in the ArrayList containing the objects. But it does not work. Java Excel搜索和ArrayList未正常工作。 It simply just adds them as new objects in the list! And I do not understand why. Java Excel搜索和ArrayList未正常工作。

I know that I may could have used iterators, or that there are probably better ways to do what I want, but my knowledge of them is simply not good enough.

I hope you guys could understand my problem and can help me out. Java Excel搜索和ArrayList未正常工作。

Sincerely yours,
Shathos

UPDATE:

Thanks to the help of Hades, I managed to find a solution to my problem! I replaced both ArrayLists with HashMaps and adjusted the customer class. Wohooo! Everything works for me! Thank you very much, Hades! Here is my working function:

    public HashMap<String, Customer> readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
        
        HashMap<String, Customer> map = new HashMap<String, Customer>();
        
        FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
        XSSFWorkbook wb = new XSSFWorkbook(inputStream);
        XSSFSheet sheet = wb.getSheetAt(0);
        
        int fr = sheet.getRow(1).getRowNum();
        XSSFRow row;
        
        while(fr < sheet.getLastRowNum()){
            
            row = sheet.getRow(fr);
            if(!row.getCell(0).getStringCellValue().equals("")){
                String

<details>
<summary>英文:</summary>

I have trouble with my code. 

It does not work the way I wanted it to work.
Sorry if I explain myself so badly. :(

(I use Apache POI version 4.1.1)

My problem: I have an excel-sheet with multiple entries. Each entry contains, among other things, the shipping date in column 1, the customer name in column 2, the pallet ID in column 15 and the shipping status in column 16. 
On top of everything, there is a headline for the columns.

The excel contains multiple customers and some of them multiple times, the only difference are the pallet IDs.
My customer object class contains, among other things, a array list, which may contain multiple, different, pallet IDs.

I want list each customer, which shipping is today, only once, but with all his pallet IDs.

My customer class looks like this:

import java.util.ArrayList;
import java.util.Date;

public class Customer {

private String customername;
private ArrayList&lt;String&gt; pallet = new ArrayList&lt;&gt;();
private String date;
private String status;
public Customer(){
}
public String getCustomerName() {
return customername;
}
public void setCustomerName(String customername) {
this.customername = customername;
}
public String getFromPallet(int i) {
return pallet.get(i);
}
public ArrayList&lt;String&gt; getPallet() {
return pallet;
}
public ArrayList&lt;String&gt; get(int j) {
return pallet;
}
public void setPallet(ArrayList&lt;String&gt; pallet) {
this.pallet = pallet;
}
public void addPallet(String string) {
this.pallet.add(string);
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String toString(){
return String.format(&quot;%1s - %2s - %3s&quot;, customername, date, status);
}    

}


my main class looks like this:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class NewTrueSimpleExcelReader {

SimpleDateFormat SDF = new SimpleDateFormat(&quot;dd.MM.yyyy&quot;);
Date today = new Date();
Date BBB;
int lastfound = 0;
public List&lt;Customer&gt; readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
List&lt;Customer&gt; listCustomers = new ArrayList&lt;&gt;();
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow firstrow = sheet.getRow(1);
int fr = firstrow.getRowNum();
XSSFRow row;
while(fr &lt; sheet.getLastRowNum()){
row = sheet.getRow(fr);
if(!row.getCell(0).getStringCellValue().equals(&quot;&quot;)){
String AAA = row.getCell(0).getStringCellValue().trim();
BBB = SDF.parse(AAA);
if((SDF.format(BBB)).equals(SDF.format(today))){
Customer aCustomer = new Customer();
System.out.println(&quot;Customer: &quot;+row.getCell(1).getStringCellValue().trim());
if(listCustomers.size() &gt; 0 &amp;&amp; row.getCell(16) == null){
for(int i=0; i&lt;listCustomers.size(); i++){
System.out.println(&quot;i is: &quot;+i);
String string1 = listCustomers.get(i).getCustomerName();
String string2 = row.getCell(1).getStringCellValue();
if(string1.equals(string2) &amp;&amp; row.getRowNum() != lastfound){
lastfound = row.getRowNum();
System.out.println(&quot;Dupl found&quot;);
for(int j=0; j&lt;listCustomers.get(i).get(j).size(); j++){
if(!row.getCell(15).getStringCellValue().trim().equals(listCustomers.get(i).getFromPallet(j))){
listCustomers.get(i).addPallet(row.getCell(15).getStringCellValue());
}
}
} else{
if(row.getRowNum() != lastfound){
System.out.println(&quot;second+: &quot;+row.getCell(0).getStringCellValue().trim());
lastfound = row.getRowNum();
aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
aCustomer.setStatus(&quot;unused&quot;);
if(row.getCell(15) != null){ //&amp;&amp; !string1.equals(string2)
aCustomer.addPallet(row.getCell(15).getStringCellValue());
}
listCustomers.add(aCustomer);
}
}
}
} else{ 
if(row.getCell(16) == null){
lastfound = row.getRowNum();
System.out.println(&quot;first: &quot;+row.getCell(0).getStringCellValue().trim());
aCustomer.setDate(row.getCell(0).getStringCellValue().trim());
aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
aCustomer.setStatus(&quot;unused&quot;);
if(row.getCell(15) != null){
aCustomer.addPallet(row.getCell(15).getStringCellValue());
}
listCustomers.add(aCustomer);
}
}
}
}
fr++;  
}
System.out.println(listCustomers.size());
wb.close();
inputStream.close();
return listCustomers;
}

}


Some customers are listed multiple times with the same name on the same date, but with different pallet IDs, so my code should just add the different IDs to already existing customers in the Array List containing the objects. But it does not work. :( It simply just adds them as new objects in the list!
And I do net understand why. :(
I know that I may could have used iterators, or that there are probably better ways to to what I want, but my knowledge of them is simply not good enough.
I hope you guys could understand my problem and can help me out. :)
Sincerely yours,
Shathos
**UPDATE:**
Thanks to the help of ***Hades***, I managed to find a solution to my problem! I replaced both Array Lists with HashMaps and adjusted the customer class. Wohooo! Everything works for me! Thank you very much, ***Hades***! Here is my working function:
public HashMap&lt;String, Customer&gt; readCustomersFromExcelFile(String excelFilePath) throws IOException, ParseException {
HashMap&lt;String, Customer&gt; map = new HashMap&lt;String, Customer&gt;();
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
XSSFWorkbook wb = new XSSFWorkbook(inputStream);
XSSFSheet sheet = wb.getSheetAt(0);
int fr = sheet.getRow(1).getRowNum();
XSSFRow row;
while(fr &lt; sheet.getLastRowNum()){
row = sheet.getRow(fr);
if(!row.getCell(0).getStringCellValue().equals(&quot;&quot;)){
String AAA = row.getCell(0).getStringCellValue().trim();
BBB = SDF.parse(AAA);
if((SDF.format(BBB)).equals(SDF.format(today))){
Customer aCustomer = new Customer();
System.out.println(&quot;Customer: &quot;+row.getCell(1).getStringCellValue().trim());
if(row.getCell(15) == null || row.getCell(15).getCellType() == CellType.BLANK){
INPUTSTRING = &quot;not found&quot;;
} else {
INPUTSTRING = row.getCell(15).getStringCellValue();
}
if(map.containsKey(row.getCell(1).getStringCellValue()) &amp;&amp; (row.getCell(16) == null || row.getCell(16).getCellType() == CellType.BLANK)) { //Check the map for new customer
System.out.println(&quot;Input is: &quot;+INPUTSTRING);
if(map.get(row.getCell(1).getStringCellValue()).getPallet().containsKey(INPUTSTRING)){
System.out.println(&quot;Duplicate found! -&gt; &quot;+INPUTSTRING);
if((INPUTSTRING.equals(&quot;not found&quot;))){
System.out.println(&quot;Empty duplicate found!&quot;);
}
} else {
System.out.println(&quot;No Duplicate found! -&gt; &quot;+INPUTSTRING);
map.get(row.getCell(1).getStringCellValue()).getPallet().put(INPUTSTRING, INPUTSTRING);  //If already in map add Pallet to the Object
}
}else if(row.getCell(16) == null || row.getCell(16).getCellType() == CellType.BLANK){
aCustomer = new Customer();     //populate this with the current values
aCustomer.setCustomerName(row.getCell(1).getStringCellValue());
aCustomer.setDate(row.getCell(0).getStringCellValue());
aCustomer.setStatus(&quot;unverladen&quot;);
if(row.getCell(15) == null || row.getCell(15).getCellType() == CellType.BLANK){
System.out.println(&quot;EMPTY FOUND: &quot;+row.getCell(1).getStringCellValue());
aCustomer.addPallet(&quot;not found&quot;, &quot;not found&quot;);
} else {
aCustomer.addPallet(row.getCell(15).getStringCellValue(), row.getCell(15).getStringCellValue());
}
map.put(row.getCell(1).getStringCellValue(), aCustomer); //add to the map when customer encounter for the first time
}
}
}
fr++;
}
wb.close();
inputStream.close();
return map;
}

:)
</details>
# 答案1
**得分**: 1
创建一个如下的映射:
```java
Map<String, Customer> map = new HashMap<String, Customer>();

键将是String,即客户名称,值将是Customer类。

当满足日期条件时,将托盘添加到现有客户,如下所示:

if (map.containsKey(customerName)) { //检查新客户是否在映射中
    map.get(customerName).getPallet.add(palletId); //如果已经在映射中,将托盘添加到对象中
} else {
    customer = new Customer(); //使用当前值填充此对象
    map.put(customerName, customer); //当首次遇到客户时将其添加到映射中
}
英文:

Create a Map a below

Map&lt;String, Customer&gt; map = new HashMap&lt;String, Customer&gt;();

The key will be String i.e. customer Name and value will be Customer Class.

When your date condition satisfies then add the pallet to the existing customer like below

if(map.containsKey(customerName)) { //Check the map for new customer
map.get(customerName).getPallet.add(palletId);  //If already in map add Pallet to the Object
}else {
customers = new Customer();     //populate this with the current values
map.put(customerName, customer); //add to the map when customer encounter for the first time
}

huangapple
  • 本文由 发表于 2020年8月13日 03:54:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63383852.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定