java中的低效循环

时间:2015-03-02 13:38:48

标签: java loops csv

这是我的csv数据:

Name,Code,Price,Colour,Type,Stock
A,1001,35000,Red,Car Paint,54
B,1002,56000,Blue,House Paint,90

如您所见,我的编码效率低下。

这是因为netbeans中的所有文本字段都不允许使用相同的变量名称,我必须为每个文本字段指定不同的变量名称(例如:code1,code2,code3,name1,name2,name3)

有人可以帮助我如何循环这些数据,以便他们这样做四次,我不必重复编码?如果字段为空,则跳过该过程。

以下是我的编码:

try
    {
       for(int z=0; z<4;z++)
       {
        String code1;
        code1=this.text1.getText();
        System.out.println("this is the code: " + code1);
        String qty;
        int qty1;
        qty=this.quantity1.getText();
        qty1=Integer.parseInt(qty);
        System.out.println("quantity: "+qty1);

        String code2;
        code2=this.text2.getText();
        System.out.println("this is the code: " + code2);
        int qty2;
        qty=this.quantity2.getText();
        qty2=Integer.parseInt(qty);
        System.out.println("quantity: "+qty2);

        String code3;
        code3=this.text3.getText();
        System.out.println("this is the code: " + code3);
        int qty3;
        qty=this.quantity2.getText();
        qty3=Integer.parseInt(qty);
        System.out.println("quantity: "+qty3);

        String code4;
        code4=this.text4.getText();
        System.out.println("this is the code: " + code4);
        int qty4;
        qty=this.quantity2.getText();
        qty4=Integer.parseInt(qty);
        System.out.println("quantity: "+qty4);

        int sum=0;

        BufferedReader line = new BufferedReader(new FileReader(new File("C:\\Users\\Laura Sutardja\\Documents\\IB DP\\Computer Science HL\\cs\\product.txt")));
        String indata;

        ArrayList<String[]> dataArr = new ArrayList<>();
        String[] club = new String[6];
        String[] value;
        while ((indata = line.readLine()) != null) {
            value = indata.split(",");
            dataArr.add(value);
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code1)) {
                System.out.println("Found name.");
                name1.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty1*price;
                String total=Integer.toString(totalprice);
                price1.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code2)) {
                System.out.println("Found name.");
                name2.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty2*price;
                String total=Integer.toString(totalprice);
                price2.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code3)) {
                System.out.println("Found name.");
                name3.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty3*price;
                int totalprice3=totalprice;
                String total=Integer.toString(totalprice);
                price3.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }

        for (int i = 0; i < dataArr.size(); i++) {
            String[] nameData = dataArr.get(i);
            if (nameData[1].equals(code4)) {
                System.out.println("Found name.");
                name4.setText(""+ nameData[0]);
                int price;
                price=Integer.parseInt(nameData[2]);
                int totalprice=qty4*price;
                int totalprice4=totalprice;
                String total=Integer.toString(totalprice);
                price4.setText(total);
                sum=sum+totalprice;
                break;
            } 
        }


       total1.setText("Rp. "+sum);
    }
    }

    catch ( IOException iox )
    {
        System.out.println("Error");
    }

2 个答案:

答案 0 :(得分:1)

为什么不使用像http://commons.apache.org/proper/commons-csv/

这样的库

答案 1 :(得分:0)

如果将其分解为单独的部分,解决此问题实际上是相当直接的。

首先,您需要解决将数据加载到易于使用的内部数据表示中的问题。将文件加载到Java中非常简单,您已经完成了这个:

BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
    System.out.println(line);
}   
csvFile.close();

下一个问题是分割线并以有意义的方式存储 - 对于每一行。

HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
for(int i=0;i<raw.length; i++){
    record.put(i, raw[i]);
}

现在您声明您只想存储具有非空字段的记录,因此我们需要检查:

HashMap<Integer, String> record = new HashMap<Integer, String>();
String[] raw = line.split(",");
Boolean store = true;
for(int i=0;i<raw.length; i++){
    if(raw[i].equals("") || raw[i].equals(null)){
        store = false;
        break;
    }
    record.put(i, raw[i]);
}           
if(store)
    csvData.add(record);

现在,您可以将csv文件的每条记录加载为可以轻松使用的字典。剩下的就是保存这些词典的列表。

ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();

BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
String line = "start";
int count = 0;
while((line = csvFile.readLine()) != null){
    if(count == 0){//skip first line
        count++;
        continue;
    }

    HashMap<Integer, String> record = new HashMap<Integer, String>();
    String[] raw = line.split(",");
    Boolean store = true;
    for(int i=0;i<raw.length; i++){
        if(raw[i].equals("") || raw[i].equals(null))
        {
            store = false;
            break;
        }
        record.put(i, raw[i]);
    }
    if(store)
        csvData.add(record);
    }   
csvFile.close();

完整的代码段,可以加载数据并轻松访问您想要的任何信息:

public class Main {
public static final int NAME = 0;
public static final int CODE = 1;
public static final int PRICE = 2;
public static final int COLOR = 3;
public static final int TYPE = 4;
public static final int STOCK = 5;

public static void main(String[] args) throws IOException{
    ArrayList<Map<Integer, String>> csvData = loadCSVFile("C:\\path\\to\\file\\products.txt");

    //Print some of the data
    System.out.println("---------------------------");
    for(Map<Integer, String> record : csvData){
        printInfo(record);
    }
}

public static ArrayList<Map<Integer, String>> loadCSVFile(String path) throws IOException{
    ArrayList<Map<Integer, String>> csvData = new ArrayList<Map<Integer, String>>();

    BufferedReader csvFile = new BufferedReader(new FileReader(new File(path)));
    String line = "start";
    int count = 0;
    while((line = csvFile.readLine()) != null){
        if(count == 0){
            count++;
            continue;
        }

        HashMap<Integer, String> record = new HashMap<Integer, String>();
        String[] raw = line.split(",");
        Boolean store = true;
        for(int i=0;i<raw.length; i++){
            if(raw[i].equals("") || raw[i].equals(null))
            {
                store = false;
                break;
            }
            record.put(i, raw[i]);
        }

        if(store)
            csvData.add(record);
    }   
    csvFile.close();
    return csvData;
}

public static void printInfo(Map<Integer, String> record){
    System.out.println(record.get(CODE) + " : " + record.get(TYPE));
    System.out.println(record.get(NAME) + " : " + record.get(STOCK) + " : " + record.get(PRICE));
    System.out.println("---------------------------");
}

}