簡體   English   中英

Java中無效的循環

[英]inefficient looping in java

這是我的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");
    }

如果將問題分解為單獨的部分,實際上解決此問題將非常簡單。

首先,您需要解決將數據加載到易於使用的內部數據表示形式中的問題。 將文件加載到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("---------------------------");
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM